package com.dhcc.sdc.api.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.dhcc.cdc.busi.dao.*;
import com.dhcc.cdc.busi.entity.*;
import com.dhcc.cdc.busi.service.*;
import com.dhcc.cdc.sys.cache.config.CdcConfigCache;
import com.dhcc.cdc.sys.dao.CdcDataMapper;
import com.dhcc.cdc.sys.entity.CdcDataEntity;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.websocket.service.MyWebSocketHandler;
import com.dhcc.sdc.api.service.ISdsReceiveDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("sdsReceiveDataService")
public class SdsDataReceiveServiceImpl implements ISdsReceiveDataService {
    @Autowired
    private ICdcEpisodeService cdcEpisodeService;//就诊信息服务类
    @Autowired
    private ICdcDiagnosisService cdcDiagnosisService;//诊断信息服务类
    @Autowired
    private ICdcSurgeryService cdcSurgeryService;//手术信息服务类
    @Autowired
    private ICdcOrdexecService cdcOrdexecService;//医嘱信息服务类
    @Autowired
    private ICdcTransferService cdcTransferService;//转科信息服务类
    @Autowired
    private ICdcEmrService cdcEmrService;//电子病历信息服务类
    @Autowired
    private ICdcBsEmrStructService cdcBsEmrStructService;//病历结构化服务类
    @Autowired
    private ICdcExaminationService cdcExaminationService;//检查报告信息服务类
    @Autowired
    private ICdcLabSetService cdcLabSetService;
    @Autowired
    private ICdcLabVisitService cdcLabVisitService;//检验样本信息服务类
    @Autowired
    private ICdcLabReportService cdcLabReportService;//检验报告信息服务类
    @Autowired
    private ICdcLabResultService cdcLabResultService;//检验结果信息服务类
    @Autowired
    private ICdcNurService cdcNurService;//护理信息服务类
    @Autowired
    private ICdcSignService cdcSignService;//症状体征信息服务类
    @Autowired
    private ICdcLabItemService cdcLabItemService;//检验字典信息服务类
    @Autowired
    private CdcDataMapper cdcDataMapper;
    @Autowired
    private CdcDictypeMapper cdcDictypeMapper;
    @Autowired
    private CdcDictionaryMapper cdcDictionaryMapper;
    @Autowired
    private CdcHospitalMapper cdcHospitalMapper;
    @Autowired
    private CdcLocationMapper cdcLocationMapper;
    @Autowired
    private CdcOrdmastMapper cdcOrdmastMapper;
    @Autowired
    private CdcOrdexecMapper cdcOrdexecMapper;
    @Autowired
    private CdcLabOrderMapper cdcLabOrderMapper;
    @Autowired
    private CdcLabEntryMapper cdcLabEntryMapper;
    @Autowired
    private CdcUserMapper cdcUserMapper;

    @Override
    public void handlePaAdmInfo(String dataStr) throws Exception{
            JSONObject dataJObj = JSONObject.parseObject(dataStr);
            if (CommonUtil.isEmpty(dataJObj.get("xcode"))) {
                return;
            }
            String xcode = dataJObj.getString("xcode"); // 索引码
            String episodeNum = dataJObj.getString("episode_num"); // 就诊号
            String patientCode = dataJObj.getString("patient_code"); // 患者主索引
            String busAdmTypeCode = dataJObj.getString("bus_adm_type_code"); // 就诊类型代码
            String busAdmTypeDesc = dataJObj.getString("bus_adm_type_desc"); // 就诊类型描述
            String busAdmStatusCode = dataJObj.getString("bus_adm_status_code"); // 就诊状态代码
            String busAdmStatusDesc = dataJObj.getString("bus_adm_status_desc"); // 就诊状态描述
            String healthCode = dataJObj.getString("health_code"); // 电子健康码
            String name = dataJObj.getString("name"); // 姓名
            String gender = dataJObj.getString("gender"); // 性别
            String nationality = dataJObj.getString("nationality"); // 国籍
            String nation = dataJObj.getString("nation"); // 民族
            String birthdate = dataJObj.getString("birthdate"); // 出生日期
            String age = dataJObj.getString("age"); // 年龄
            String cardType = dataJObj.getString("card_type"); // 证件类型
            String cardValue = dataJObj.getString("card_value"); // 证件号码
            String nativePlace = dataJObj.getString("native_place"); // 籍贯
            String company = dataJObj.getString("company"); // 工作单位
            String telephone = dataJObj.getString("telephone"); // 电话
            String email = dataJObj.getString("email"); // 邮箱
            String contactor = dataJObj.getString("contactor"); // 联系人
            String contactorPhone = dataJObj.getString("contactor_phone"); // 联系人电话
            String career = dataJObj.getString("career"); // 职业
            String bloodAbo = dataJObj.getString("blood_abo"); // 血型ABO
            String bloodRh = dataJObj.getString("blood_rh"); // 血型Rh
            String registNum = dataJObj.getString("regist_num"); // 登记号
            String mrNum = dataJObj.getString("mr_num"); // 病案号
            String nativeProvince = dataJObj.getString("native_province"); // 户籍所在地省
            String nativeCity = dataJObj.getString("native_city"); // 户籍所在地市
            String nativeCounty = dataJObj.getString("native_county"); // 户籍所在地县
            String nativeTown = dataJObj.getString("native_town"); // 户籍所在地乡
            String nativeAddress = dataJObj.getString("native_address"); // 户籍所在地详细描述
            String nowProvince = dataJObj.getString("now_province"); // 现住址所在地省
            String nowCity = dataJObj.getString("now_city"); // 现住址所在地市
            String nowCounty = dataJObj.getString("now_county"); // 现住址所在地县
            String nowTown = dataJObj.getString("now_town"); // 现住址所在地乡
            String nowAddress = dataJObj.getString("now_address"); // 现住址详细描述
            String isDeath = dataJObj.getString("is_death"); // 死亡标志
            String deathDate = dataJObj.getString("death_date"); // 死亡日期
            String admNums = dataJObj.getString("adm_nums"); // 住院次数
            String admDate = dataJObj.getString("adm_date"); // 入院日期
            String admHospId = dataJObj.getString("adm_hosp_id"); // 就诊院区代码
            String admHospName = dataJObj.getString("adm_hosp_name"); // 就诊院区名称
            String admLocId = dataJObj.getString("adm_loc_id"); // 就诊科室代码
            String admLocName = dataJObj.getString("adm_loc_name"); // 就诊科室名称
            String admWardId = dataJObj.getString("adm_ward_id"); // 就诊病区代码
            String admWardName = dataJObj.getString("adm_ward_name"); // 就诊病区名称
            String admRoom = dataJObj.getString("adm_room"); // 就诊房间
            String admBed = dataJObj.getString("adm_bed"); // 就诊床位
            String admDoctorId = dataJObj.getString("adm_doctor_id"); // 主管医生ID
            String admDoctorName = dataJObj.getString("adm_doctor_name"); // 主管医生姓名
            String dishDate = dataJObj.getString("dish_date"); // 出院日期
            String admDays = dataJObj.getString("adm_days"); // 住院天数
            String isNewly = dataJObj.getString("is_newly"); // 是否初诊
            String isNewborn = dataJObj.getString("is_newborn"); // 是否新生儿
            String bornWeight = dataJObj.getString("born_weight"); // 新生儿出生体重(g)
            String admWeight = dataJObj.getString("adm_weight"); // 新生儿入院体重(g)
            // 验证就诊表中是否已有该次就诊信息
            CdcEpisodeEntity episode = new CdcEpisodeEntity();
            episode.setXcode(xcode);
            //获取dataid
            CdcDataEntity cdcDataEntity = new CdcDataEntity();
            cdcDataEntity.setCode("PatData");
            cdcDataEntity.setIsActive(1);
            cdcDataEntity = cdcDataMapper.selectOne(cdcDataEntity);
            Long dataId = cdcDataEntity.getId();
            //获取basedataid
            CdcDataEntity cdcDataEntityBase = new CdcDataEntity();
            cdcDataEntityBase.setCode("HisBase");
            cdcDataEntityBase.setIsActive(1);
            cdcDataEntityBase = cdcDataMapper.selectOne(cdcDataEntityBase);
            Long basedataId = cdcDataEntityBase.getId();

            if (CommonUtil.isNotEmpty(episodeNum)) {
                episode.setEpisodeNum(episodeNum);
            } else {
                episode.setEpisodeNum(xcode);
            }
            if (CommonUtil.isNotEmpty(patientCode)) {
                episode.setPatientCode(patientCode);
            }
            //获取ID
            if (CommonUtil.isNotEmpty(busAdmTypeCode)) {
                CdcDictypeEntity cdcDictypeEntity = new CdcDictypeEntity();
                cdcDictypeEntity.setCode("bus_adm_type");
                cdcDictypeEntity.setIsActive(1);
                cdcDictypeEntity = cdcDictypeMapper.selectOne(cdcDictypeEntity);
                Long dicadmtypeId = cdcDictypeEntity.getId();
                //如果没有对应ID就要插入
                CdcDictionaryEntity cdcDictionaryEntity = new CdcDictionaryEntity();
                cdcDictionaryEntity.setDictypeId(dicadmtypeId);
                cdcDictionaryEntity.setCode(busAdmTypeCode);
                cdcDictionaryEntity.setIsActive(1);
                cdcDictionaryEntity = cdcDictionaryMapper.selectOne(cdcDictionaryEntity);
                //判断字典存在不存在
                Long busadmtypeid = null;
                if (CommonUtil.isEmpty(cdcDictionaryEntity)) {//不存在  保存到字典表里面
                    cdcDictionaryEntity = new CdcDictionaryEntity();
                    cdcDictionaryEntity.setDictypeId(dicadmtypeId);
                    cdcDictionaryEntity.setCode(busAdmTypeCode);
                    cdcDictionaryEntity.setDescription(busAdmTypeDesc);
                    cdcDictionaryEntity.setIsActive(1);
                    //保存字典
                    cdcDictionaryMapper.insert(cdcDictionaryEntity);
                    busadmtypeid = cdcDictionaryEntity.getId();
                } else {
                    busadmtypeid = cdcDictionaryEntity.getId();
                }
                episode.setBusAdmTypeId(busadmtypeid);
            }
            //获取ID
            if (CommonUtil.isNotEmpty(busAdmStatusCode)) {
                CdcDictypeEntity cdcDictypeEntity = new CdcDictypeEntity();
                cdcDictypeEntity.setCode("bus_adm_status");
                cdcDictypeEntity.setIsActive(1);
                cdcDictypeEntity = cdcDictypeMapper.selectOne(cdcDictypeEntity);
                Long dicadmstatusId = cdcDictypeEntity.getId();
                //如果没有对应ID就要插入
                CdcDictionaryEntity cdcDictionaryEntity = new CdcDictionaryEntity();
                cdcDictionaryEntity.setDictypeId(dicadmstatusId);
                cdcDictionaryEntity.setCode(busAdmStatusCode);
                cdcDictionaryEntity.setIsActive(1);
                cdcDictionaryEntity = cdcDictionaryMapper.selectOne(cdcDictionaryEntity);
                //判断字典存在不存在
                Long busadmstatusid = null;
                if (CommonUtil.isEmpty(cdcDictionaryEntity)) {//不存在  保存到字典表里面
                    cdcDictionaryEntity = new CdcDictionaryEntity();
                    cdcDictionaryEntity.setDictypeId(dicadmstatusId);
                    cdcDictionaryEntity.setCode(busAdmStatusCode);
                    cdcDictionaryEntity.setDescription(busAdmStatusDesc);
                    cdcDictionaryEntity.setIsActive(1);
                    //保存字典
                    cdcDictionaryMapper.insert(cdcDictionaryEntity);
                    busadmstatusid = cdcDictionaryEntity.getId();
                } else {
                    busadmstatusid = cdcDictionaryEntity.getId();
                }
                episode.setBusAdmStatusId(busadmstatusid);
            }
            if (CommonUtil.isNotEmpty(healthCode)) {
                episode.setHealthCode(healthCode);
            }

            if (CommonUtil.isNotEmpty(name)) {
                episode.setName(name);
            }
            if (CommonUtil.isNotEmpty(gender)) {
                episode.setGender(gender);
            }
            if (CommonUtil.isNotEmpty(nationality)) {
                episode.setNationality(nationality);
            }
            if (CommonUtil.isNotEmpty(nation)) {
                episode.setNation(nation);
            }
            if (CommonUtil.isNotEmpty(birthdate)) {
                Date birthdatef = getTime(birthdate);
                episode.setBirthdate(birthdatef);
            }
            if (CommonUtil.isNotEmpty(age)) {
                if (age.contains("岁")) {
                    age = age.split("岁")[0];
                } else if (age.contains("月")) {
                    age = "0";
                } else if (age.contains("天")) {
                    age = "0";
                }
                episode.setAge(age);
            }
            if (CommonUtil.isNotEmpty(cardType)) {
                episode.setCardType(cardType);
            }
            if (CommonUtil.isNotEmpty(cardValue)) {
                episode.setCardValue(cardValue);
            }
            if (CommonUtil.isNotEmpty(nativePlace)) {
                episode.setNativePlace(nativePlace);
            }
            if (CommonUtil.isNotEmpty(company)) {
                episode.setCompany(company);
            }
            if (CommonUtil.isNotEmpty(telephone)) {
                episode.setTelephone(telephone);
            }
            if (CommonUtil.isNotEmpty(email)) {
                episode.setEmail(email);
            }
            if (CommonUtil.isNotEmpty(contactor)) {
                episode.setContactor(contactor);
            }
            if (CommonUtil.isNotEmpty(contactorPhone)) {
                episode.setContactorPhone(contactorPhone);
            }
            if (CommonUtil.isNotEmpty(career)) {
                episode.setCareer(career);
            }
            if (CommonUtil.isNotEmpty(bloodAbo)) {
                episode.setBloodAbo(bloodAbo);
            }
            if (CommonUtil.isNotEmpty(bloodRh)) {
                episode.setBloodRh(bloodRh);
            }
            if (CommonUtil.isNotEmpty(registNum)) {
                episode.setRegistNum(registNum);
            }
            if (CommonUtil.isNotEmpty(mrNum)) {
                episode.setMrNum(mrNum);
            }
            if (CommonUtil.isNotEmpty(nativeProvince)) {
                episode.setNativeProvince(nativeProvince);
            }
            if (CommonUtil.isNotEmpty(nativeCity)) {
                episode.setNativeCity(nativeCity);
            }
            if (CommonUtil.isNotEmpty(nativeCounty)) {
                episode.setNativeCounty(nativeCounty);
            }
            if (CommonUtil.isNotEmpty(nativeTown)) {
                episode.setNativeTown(nativeTown);
            }
            if (CommonUtil.isNotEmpty(nativeAddress)) {
                episode.setNativeAddress(nativeAddress);
            }
            if (CommonUtil.isNotEmpty(nowProvince)) {
                episode.setNowProvince(nowProvince);
            }
            if (CommonUtil.isNotEmpty(nowCity)) {
                episode.setNowCity(nowCity);
            }
            if (CommonUtil.isNotEmpty(nowCounty)) {
                episode.setNowCounty(nowCounty);
            }
            if (CommonUtil.isNotEmpty(nowTown)) {
                episode.setNowTown(nowTown);
            }
            if (CommonUtil.isNotEmpty(nowAddress)) {
                episode.setNowAddress(nowAddress);
            }
            if (CommonUtil.isNotEmpty(isDeath)) {
                Integer isdeathf = Integer.valueOf(isDeath);
                episode.setIsDeath(isdeathf);
            }
            if (CommonUtil.isNotEmpty(deathDate)) {
                Date deathdatef = getTime(deathDate);
                episode.setDeathDate(deathdatef);
            }
            if (CommonUtil.isNotEmpty(admNums)) {
                Integer admnumsf = Integer.valueOf(admNums);
                episode.setAdmNums(admnumsf);
            }
            if (CommonUtil.isNotEmpty(admDate)) {
                Date admdatef = getTime(admDate);
                episode.setAdmDate(admdatef);
            }
            //获取ID
            if (CommonUtil.isNotEmpty(admLocId)) {
                CdcLocationEntity cdcLocationEntity = new CdcLocationEntity();
                cdcLocationEntity.setXcode(admLocId);
                cdcLocationEntity.setDataId(basedataId);
                cdcLocationEntity.setIsActive(1);
                cdcLocationEntity = cdcLocationMapper.selectOne(cdcLocationEntity);

                //判断科室存在不存在
                Long locid = null;
                if (CommonUtil.isEmpty(cdcLocationEntity)) {//不存在  保存到科室表里面
                    cdcLocationEntity = new CdcLocationEntity();
                    cdcLocationEntity.setXcode(admLocId);
                    cdcLocationEntity.setIsActive(1);
                    cdcLocationEntity.setCode("newdep" + admLocId);
                    cdcLocationEntity.setDescription(admLocName);
                    cdcLocationEntity.setDataId(basedataId);
                    CdcHospitalEntity cdcHospitalEntity = new CdcHospitalEntity();
                    cdcHospitalEntity.setXcode(admHospId);
                    cdcHospitalEntity.setDataId(basedataId);
                    cdcHospitalEntity.setIsActive(1);
                    cdcHospitalEntity = cdcHospitalMapper.selectOne(cdcHospitalEntity);
                    Long hospid = null;
                    if (CommonUtil.isEmpty(cdcHospitalEntity)) {//不存在  保存到院区表里面
                        cdcHospitalEntity = new CdcHospitalEntity();
                        cdcHospitalEntity.setXcode(admHospId);
                        cdcHospitalEntity.setIsActive(1);
                        cdcHospitalEntity.setDataId(basedataId);
                        cdcHospitalEntity.setCode("newhosp" + admHospId);
                        cdcHospitalEntity.setDescription(admHospName);
                        cdcHospitalMapper.insert(cdcHospitalEntity);
                        hospid = cdcHospitalEntity.getId();
                    } else {
                        hospid = cdcHospitalEntity.getId();
                    }
                    cdcLocationEntity.setHospitalId(hospid);

                    //保存科室
                    cdcLocationMapper.insert(cdcLocationEntity);
                }
                episode.setAdmLocId(Long.valueOf(admLocId));
            }
            if (CommonUtil.isNotEmpty(admLocName)) {
                episode.setAdmLocDesc(admLocName);
            }
            //获取ID
            if (CommonUtil.isNotEmpty(admWardId)) {
                CdcLocationEntity cdcLocationEntity = new CdcLocationEntity();
                cdcLocationEntity.setXcode(admWardId);
                cdcLocationEntity.setDataId(basedataId);
                cdcLocationEntity.setIsActive(1);
                cdcLocationEntity = cdcLocationMapper.selectOne(cdcLocationEntity);
                //判断病区存在不存在
                if (CommonUtil.isEmpty(cdcLocationEntity)) {//不存在  保存到科室表里面
                    cdcLocationEntity = new CdcLocationEntity();
                    cdcLocationEntity.setXcode(admWardId);
                    cdcLocationEntity.setIsActive(1);
                    cdcLocationEntity.setCode("newward" + admWardId);
                    cdcLocationEntity.setDescription(admWardName);
                    cdcLocationEntity.setDataId(basedataId);

                    CdcHospitalEntity cdcHospitalEntity = new CdcHospitalEntity();
                    cdcHospitalEntity.setXcode(admHospId);
                    cdcHospitalEntity.setDataId(basedataId);
                    cdcHospitalEntity.setIsActive(1);
                    cdcHospitalEntity = cdcHospitalMapper.selectOne(cdcHospitalEntity);
                    Long hospid = null;
                    if (CommonUtil.isEmpty(cdcHospitalEntity)) {//不存在  保存到院区表里面
                        cdcHospitalEntity = new CdcHospitalEntity();
                        cdcHospitalEntity.setXcode(admHospId);
                        cdcHospitalEntity.setIsActive(1);
                        cdcHospitalEntity.setDataId(basedataId);
                        cdcHospitalEntity.setCode("newhosp" + admHospId);
                        cdcHospitalEntity.setDescription(admHospName);
                        cdcHospitalMapper.insert(cdcHospitalEntity);
                        hospid = cdcHospitalEntity.getId();
                    } else {
                        hospid = cdcHospitalEntity.getId();
                    }
                    cdcLocationEntity.setHospitalId(hospid);

                    //保存科室
                    cdcLocationMapper.insert(cdcLocationEntity);
                }
                episode.setAdmWardId(Long.valueOf(admWardId));
            }
            if (CommonUtil.isNotEmpty(admWardName)) {
                episode.setAdmWardDesc(admWardName);
            }
            if (CommonUtil.isNotEmpty(admRoom)) {
                episode.setAdmRoom(admRoom);
            }
            if (CommonUtil.isNotEmpty(admBed)) {
                episode.setAdmBed(admBed);
            }
            //获取ID
            if (CommonUtil.isNotEmpty(admDoctorId)) {
                episode.setAdmDoctorId(Long.valueOf(admDoctorId));
            }
            if (CommonUtil.isNotEmpty(admDoctorName)) {
                episode.setAdmDoctorName(admDoctorName);
            }
            if (CommonUtil.isNotEmpty(dishDate)) {
                Date dishdatef = getTime(dishDate);
                episode.setDishDate(dishdatef);
            }
            if (CommonUtil.isNotEmpty(admDays)) {
                Integer admdaysf = Integer.valueOf(admDays);
                episode.setAdmDays(admdaysf);
            }

            if (CommonUtil.isNotEmpty(isNewly)) {
                Integer isnewlyf = Integer.valueOf(isNewly);
                episode.setIsNewly(isnewlyf);
            }

            if (CommonUtil.isNotEmpty(isNewborn)) {
                Integer isnewbornf = Integer.valueOf(isNewborn);
                episode.setIsNewborn(isnewbornf);
            }

            if (CommonUtil.isNotEmpty(bornWeight)) {
                Integer bornweightf = Integer.valueOf(bornWeight);
                episode.setBornWeight(bornweightf);
            }

            if (CommonUtil.isNotEmpty(admWeight)) {
                Integer admweightf = Integer.valueOf(admWeight);
                episode.setAdmWeight(admweightf);
            }
            episode.setDataId(dataId);
            episode.setIsActive(1);

            //先判断是不是有该对象
            Wrapper<CdcEpisodeEntity> wrapper = new EntityWrapper<CdcEpisodeEntity>();
            wrapper.eq("xcode", episode.getXcode());
            wrapper.eq("data_id", dataId);
            int count = cdcEpisodeService.selectCount(wrapper);
            if (count > 0) {
                cdcEpisodeService.update(episode, wrapper);
            } else {
                cdcEpisodeService.insert(episode);
            }
        //发送消息推送给前端刷新页面
        MyWebSocketHandler.fanoutMessage(xcode);
    }

    @Override
    public void handlDiagInfo(String dataStr) throws Exception{
            JSONObject diagJObj = JSONObject.parseObject(dataStr);
            String episodeId = diagJObj.getString("episode_id"); // 就诊号
            //获取episodeid
            Wrapper<CdcEpisodeEntity> wrapper = new EntityWrapper<CdcEpisodeEntity>();
            wrapper.eq("xcode", episodeId);
            CdcEpisodeEntity cdcEpisodeEntity = cdcEpisodeService.selectOne(wrapper);//就诊信息没在库中？？？
            if (CommonUtil.isEmpty(cdcEpisodeEntity)) {
                return;
            }
            Long epsid = cdcEpisodeEntity.getId();
            //获取dataid
            CdcDataEntity cdcDataEntity = new CdcDataEntity();
            cdcDataEntity.setCode("DiagData");
            cdcDataEntity.setIsActive(1);
            cdcDataEntity = cdcDataMapper.selectOne(cdcDataEntity);
            Long dataId = cdcDataEntity.getId();
            //先删除全部诊断
            Wrapper<CdcDiagnosisEntity> wrapper1 = new EntityWrapper<CdcDiagnosisEntity>();
            wrapper1.eq("episode_id", epsid);
            wrapper1.eq("data_id", dataId);
            cdcDiagnosisService.delete(wrapper1);

            JSONArray diagnosisList = diagJObj.getJSONArray("DiagnosisList");
            for (int i = 0; i < diagnosisList.size(); i++) {
                JSONObject diagnosisObj = diagnosisList.getJSONObject(i);
                String xcode = diagnosisObj.getString("xcode"); // 诊断标识
                String busDiagCateCode = diagnosisObj.getString("bus_diag_cate_code"); // 分类代码
                String busDiagCateDesc = diagnosisObj.getString("bus_diag_cate_desc"); // 分类描述
                String busDiagTypeCode = diagnosisObj.getString("bus_diag_type_code"); // 类型代码
                String busDiagTypeDesc = diagnosisObj.getString("bus_diag_type_desc"); // 类型描述
                String description = diagnosisObj.getString("description"); // 描述
                String icd10 = diagnosisObj.getString("icd10"); // ICD10
                String isMain = diagnosisObj.getString("is_main"); // 是否主诊断
                String busDiagStatusCode = diagnosisObj.getString("bus_diag_status_code"); // 状态代码
                String busDiagStatusDesc = diagnosisObj.getString("bus_diag_status_desc"); // 状态描述
                String diagDate = diagnosisObj.getString("diag_date"); // 诊断日期
                String attackDate = diagnosisObj.getString("attack_date"); // 发病日期
                String note = diagnosisObj.getString("note"); // 备注

                // 根据诊断信息进行处理或存储
                CdcDiagnosisEntity diag = new CdcDiagnosisEntity();
                diag.setEpisodeId(epsid);
                diag.setXcode(xcode);
                //获取DiagCateId
                if (CommonUtil.isNotEmpty(busDiagCateCode)) {
                    CdcDictypeEntity cdcDictypeEntity = new CdcDictypeEntity();
                    cdcDictypeEntity.setCode("bus_diag_cate");
                    cdcDictypeEntity.setIsActive(1);
                    cdcDictypeEntity = cdcDictypeMapper.selectOne(cdcDictypeEntity);
                    Long dicdiagcateId = cdcDictypeEntity.getId();
                    //如果没有对应ID就要插入
                    CdcDictionaryEntity cdcDictionaryEntity = new CdcDictionaryEntity();
                    cdcDictionaryEntity.setDictypeId(dicdiagcateId);
                    cdcDictionaryEntity.setCode(busDiagCateCode);
                    cdcDictionaryEntity.setIsActive(1);
                    cdcDictionaryEntity = cdcDictionaryMapper.selectOne(cdcDictionaryEntity);
                    //判断字典存在不存在
                    Long busdiagcateid = null;
                    if (CommonUtil.isEmpty(cdcDictionaryEntity)) {//不存在  保存到字典表里面
                        cdcDictionaryEntity = new CdcDictionaryEntity();
                        cdcDictionaryEntity.setDictypeId(dicdiagcateId);
                        cdcDictionaryEntity.setCode(busDiagCateCode);
                        cdcDictionaryEntity.setDescription(busDiagCateDesc);
                        cdcDictionaryEntity.setIsActive(1);
                        //保存字典
                        cdcDictionaryMapper.insert(cdcDictionaryEntity);
                        busdiagcateid = cdcDictionaryEntity.getId();
                    } else {
                        busdiagcateid = cdcDictionaryEntity.getId();
                    }
                    diag.setBusDiagCateId(busdiagcateid);
                }

                //获取ID
                if (CommonUtil.isNotEmpty(busDiagTypeCode)) {
                    CdcDictypeEntity cdcDictypeEntity = new CdcDictypeEntity();
                    cdcDictypeEntity.setCode("bus_diag_type");
                    cdcDictypeEntity.setIsActive(1);
                    cdcDictypeEntity = cdcDictypeMapper.selectOne(cdcDictypeEntity);
                    Long dicdiagtypeId = cdcDictypeEntity.getId();
                    //如果没有对应ID就要插入
                    CdcDictionaryEntity cdcDictionaryEntity = new CdcDictionaryEntity();
                    cdcDictionaryEntity.setDictypeId(dicdiagtypeId);
                    cdcDictionaryEntity.setCode(busDiagTypeCode);
                    cdcDictionaryEntity.setIsActive(1);
                    cdcDictionaryEntity = cdcDictionaryMapper.selectOne(cdcDictionaryEntity);
                    //判断字典存在不存在
                    Long busdiagtypeid = null;
                    if (CommonUtil.isEmpty(cdcDictionaryEntity)) {//不存在  保存到字典表里面
                        cdcDictionaryEntity = new CdcDictionaryEntity();
                        cdcDictionaryEntity.setDictypeId(dicdiagtypeId);
                        cdcDictionaryEntity.setCode(busDiagTypeCode);
                        cdcDictionaryEntity.setDescription(busDiagTypeDesc);
                        cdcDictionaryEntity.setIsActive(1);
                        //保存字典
                        cdcDictionaryMapper.insert(cdcDictionaryEntity);
                        busdiagtypeid = cdcDictionaryEntity.getId();
                    } else {
                        busdiagtypeid = cdcDictionaryEntity.getId();
                    }
                    diag.setBusDiagTypeId(busdiagtypeid);
                }

                if (CommonUtil.isNotEmpty(description)) {
                    diag.setDescription(description);
                }
                if (CommonUtil.isNotEmpty(icd10)) {
                    diag.setIcd10(icd10);
                }
                if (CommonUtil.isNotEmpty(isMain)) {
                    Integer ismainf = Integer.valueOf(isMain);
                    diag.setIsMain(ismainf);
                }
                //获取ID
                if (CommonUtil.isNotEmpty(busDiagStatusCode)) {
                    CdcDictypeEntity cdcDictypeEntity = new CdcDictypeEntity();
                    cdcDictypeEntity.setCode("bus_diag_status");
                    cdcDictypeEntity.setIsActive(1);
                    cdcDictypeEntity = cdcDictypeMapper.selectOne(cdcDictypeEntity);
                    Long dicdiagstatusId = cdcDictypeEntity.getId();
                    //如果没有对应ID就要插入
                    CdcDictionaryEntity cdcDictionaryEntity = new CdcDictionaryEntity();
                    cdcDictionaryEntity.setDictypeId(dicdiagstatusId);
                    cdcDictionaryEntity.setCode(busDiagStatusCode);
                    cdcDictionaryEntity.setIsActive(1);
                    cdcDictionaryEntity = cdcDictionaryMapper.selectOne(cdcDictionaryEntity);
                    //判断字典存在不存在
                    Long busdiagstatusid = null;
                    if (CommonUtil.isEmpty(cdcDictionaryEntity)) {//不存在  保存到字典表里面
                        cdcDictionaryEntity = new CdcDictionaryEntity();
                        cdcDictionaryEntity.setDictypeId(dicdiagstatusId);
                        cdcDictionaryEntity.setCode(busDiagStatusCode);
                        cdcDictionaryEntity.setDescription(busDiagStatusDesc);
                        cdcDictionaryEntity.setIsActive(1);
                        //保存字典
                        cdcDictionaryMapper.insert(cdcDictionaryEntity);
                        busdiagstatusid = cdcDictionaryEntity.getId();
                    } else {
                        busdiagstatusid = cdcDictionaryEntity.getId();
                    }
                    diag.setBusDiagStatusId(busdiagstatusid);
                }
                if (CommonUtil.isNotEmpty(diagDate)) {
                    Date diagdatef = getTime(diagDate);
                    diag.setDiagDate(diagdatef);
                }
                if (CommonUtil.isNotEmpty(attackDate)) {
                    Date attackdatef = getTime(attackDate);
                    diag.setAttackDate(attackdatef);
                }
                //获取ID
                CdcDictypeEntity cdcDictypeEntity = new CdcDictypeEntity();
                cdcDictypeEntity.setCode("bus_diag_source");
                cdcDictypeEntity.setIsActive(1);
                cdcDictypeEntity = cdcDictypeMapper.selectOne(cdcDictypeEntity);
                Long dicdiagsourceId = cdcDictypeEntity.getId();
                //如果没有对应ID就要插入
                CdcDictionaryEntity cdcDictionaryEntity = new CdcDictionaryEntity();
                cdcDictionaryEntity.setDictypeId(dicdiagsourceId);
                cdcDictionaryEntity.setCode("C");
                cdcDictionaryEntity.setIsActive(1);
                cdcDictionaryEntity = cdcDictionaryMapper.selectOne(cdcDictionaryEntity);
                //判断字典存在不存在
                Long busdiagsourceid = null;
                if (CommonUtil.isEmpty(cdcDictionaryEntity)) {//不存在  保存到字典表里面
                    cdcDictionaryEntity = new CdcDictionaryEntity();
                    cdcDictionaryEntity.setDictypeId(dicdiagsourceId);
                    cdcDictionaryEntity.setCode("C");
                    cdcDictionaryEntity.setDescription("临床");
                    cdcDictionaryEntity.setIsActive(1);
                    //保存字典
                    cdcDictionaryMapper.insert(cdcDictionaryEntity);
                    busdiagsourceid = cdcDictionaryEntity.getId();
                } else {
                    busdiagsourceid = cdcDictionaryEntity.getId();
                }
                diag.setBusDiagSourceId(busdiagsourceid);
                if (CommonUtil.isNotEmpty(note)) {
                    diag.setNote(note);
                }
                diag.setDataId(dataId);
                diag.setIsActive(1);

                //插入
                cdcDiagnosisService.insert(diag);
            }

        //发送消息推送给前端刷新页面
        MyWebSocketHandler.fanoutMessage(episodeId);

    }

    @Override
    public void handleOrderInfo(String dataStr) throws Exception{

            JSONObject orderJObj = JSONObject.parseObject(dataStr);
            String episodeId = orderJObj.getString("episode_id"); // 索引码
            //获取episodeid
            Wrapper<CdcEpisodeEntity> wrapper = new EntityWrapper<CdcEpisodeEntity>();
            wrapper.eq("xcode", episodeId);
            CdcEpisodeEntity cdcEpisodeEntity = cdcEpisodeService.selectOne(wrapper);//就诊信息没在库中？？？
            if (CommonUtil.isEmpty(cdcEpisodeEntity)) {
                return;
            }
            Long epsid = cdcEpisodeEntity.getId();
            //获取basedataId
            CdcDataEntity cdcDataEntityBase = new CdcDataEntity();
            cdcDataEntityBase.setCode("HisBase");
            cdcDataEntityBase.setIsActive(1);
            cdcDataEntityBase = cdcDataMapper.selectOne(cdcDataEntityBase);
            Long basedataId = cdcDataEntityBase.getId();
            //获取dataid
            CdcDataEntity cdcDataEntity = new CdcDataEntity();
            cdcDataEntity.setCode("OrdData");
            cdcDataEntity.setIsActive(1);
            cdcDataEntity = cdcDataMapper.selectOne(cdcDataEntity);
            Long dataId = cdcDataEntity.getId();

            JSONArray orderList = orderJObj.getJSONArray("OrderList");
            for (int i = 0; i < orderList.size(); i++) {
                JSONObject orderObj = orderList.getJSONObject(i);
                String xcode = orderObj.getString("xcode"); // 索引码
                String orderCode = orderObj.getString("order_code"); // 医嘱项代码
//                String orderDesc = orderObj.getString("order_desc"); // 医嘱项描述
                String busOrderStatusCode = orderObj.getString("bus_order_status_code"); // 状态代码
                String busOrderStatusDesc = orderObj.getString("bus_order_status_desc"); // 状态描述
                String orderDate = orderObj.getString("order_date"); // 开医嘱日期
                String orderLocCode = orderObj.getString("order_loc_code"); // 开医嘱科室代码
//                String orderLocName = orderObj.getString("order_loc_name"); // 开医嘱科室名称
                String recLocCode = orderObj.getString("rec_loc_code"); // 接收科室代码
//                String recLocName = orderObj.getString("rec_loc_name"); // 接收科室名称
                String startDate = orderObj.getString("start_date"); // 开始日期
                String endDate = orderObj.getString("end_date"); // 停止日期
                String busOrderInstrucCode = orderObj.getString("bus_order_instruc_code"); // 给药途径代码
                String busOrderInstrucDesc = orderObj.getString("bus_order_instruc_desc"); // 给药途径描述
                String doseQty = orderObj.getString("dose_qty"); // 单次剂量
                String busOrderUnitCode = orderObj.getString("bus_order_unit_code"); // 剂量单位代码
                String busOrderUnitDesc = orderObj.getString("bus_order_unit_desc"); // 剂量单位描述
                String busOrderFreqCode = orderObj.getString("bus_order_freq_code"); // 频次代码
                String busOrderFreqDesc = orderObj.getString("bus_order_freq_desc"); // 频次描述
                String busOrderPriorityCode = orderObj.getString("bus_order_priority_code"); // 医嘱类型代码
                String busOrderPriorityDesc = orderObj.getString("bus_order_priority_desc"); // 医嘱类型描述
                String isAnti = orderObj.getString("is_anti"); // 是否抗生素

                CdcOrdexecEntity order = new CdcOrdexecEntity();
                order.setEpisodeId(epsid);
                order.setXcode(xcode);

                // 根据医嘱信息进行处理或存储
                //根据医嘱条目id获取longid
                if (CommonUtil.isNotEmpty(orderCode)) {
                    //如果没有对应ID就要插入
                    CdcOrdmastEntity cdcOrdmastEntity = new CdcOrdmastEntity();
                    cdcOrdmastEntity.setCode(orderCode);
                    cdcOrdmastEntity.setDataId(dataId);
                    cdcOrdmastEntity.setIsActive(1);
                    cdcOrdmastEntity = cdcOrdmastMapper.selectOne(cdcOrdmastEntity);
                    if (CommonUtil.isNotEmpty(cdcOrdmastEntity)) {
                        Long orderid = cdcOrdmastEntity.getId();
                        order.setOrderMastId(orderid);
                    } else {
                        continue;
                    }
                }
                //获取orderstatusId
                if (CommonUtil.isNotEmpty(busOrderStatusCode)) {
                    CdcDictypeEntity cdcDictypeEntity = new CdcDictypeEntity();
                    cdcDictypeEntity.setCode("bus_order_status");
                    cdcDictypeEntity.setIsActive(1);
                    cdcDictypeEntity = cdcDictypeMapper.selectOne(cdcDictypeEntity);
                    Long dicorderstatusId = cdcDictypeEntity.getId();
                    //如果没有对应ID就要插入
                    CdcDictionaryEntity cdcDictionaryEntity = new CdcDictionaryEntity();
                    cdcDictionaryEntity.setDictypeId(dicorderstatusId);
                    cdcDictionaryEntity.setCode(busOrderStatusCode);
                    cdcDictionaryEntity.setIsActive(1);
                    cdcDictionaryEntity = cdcDictionaryMapper.selectOne(cdcDictionaryEntity);
                    //判断字典存在不存在
                    Long busorderstatusid = null;
                    if (CommonUtil.isEmpty(cdcDictionaryEntity)) {//不存在  保存到字典表里面
                        cdcDictionaryEntity = new CdcDictionaryEntity();
                        cdcDictionaryEntity.setDictypeId(dicorderstatusId);
                        cdcDictionaryEntity.setCode(busOrderStatusCode);
                        cdcDictionaryEntity.setDescription(busOrderStatusDesc);
                        cdcDictionaryEntity.setIsActive(1);
                        //保存字典
                        cdcDictionaryMapper.insert(cdcDictionaryEntity);
                        busorderstatusid = cdcDictionaryEntity.getId();
                    } else {
                        busorderstatusid = cdcDictionaryEntity.getId();
                    }
                    order.setBusOrderStatusId(busorderstatusid);
                }
                if (CommonUtil.isNotEmpty(orderDate)) {
                    Date orderdatef = getTime(orderDate);
                    order.setOrderDate(orderdatef);
                }
                if (CommonUtil.isNotEmpty(orderLocCode)) {
                    CdcLocationEntity cdcLocationEntity = new CdcLocationEntity();
                    cdcLocationEntity.setCode(orderLocCode);
                    cdcLocationEntity.setDataId(basedataId);
                    cdcLocationEntity.setIsActive(1);
                    cdcLocationEntity = cdcLocationMapper.selectOne(cdcLocationEntity);
                    if (CommonUtil.isNotEmpty(cdcLocationEntity)) {
                        Long orderLocLongId = cdcLocationEntity.getId();
                        order.setOrderLocId(orderLocLongId);
                    }
                }
                if (CommonUtil.isNotEmpty(recLocCode)) {
                    CdcLocationEntity cdcLocationEntity = new CdcLocationEntity();
                    cdcLocationEntity.setCode(recLocCode);
                    cdcLocationEntity.setDataId(basedataId);
                    cdcLocationEntity.setIsActive(1);
                    cdcLocationEntity = cdcLocationMapper.selectOne(cdcLocationEntity);
                    if (CommonUtil.isNotEmpty(cdcLocationEntity)) {
                        Long recLocLongId = cdcLocationEntity.getId();
                        order.setRecLocId(recLocLongId);
                    }
                }
                if (CommonUtil.isNotEmpty(startDate)) {
                    Date startdatef = getTime(startDate);
                    order.setStartDate(startdatef);
                }
                if (CommonUtil.isNotEmpty(endDate)) {
                    Date enddatef = getTime(endDate);
                    order.setOrderDate(enddatef);
                }
                //获取orderinstrucId
                if (CommonUtil.isNotEmpty(busOrderInstrucCode)) {
                    CdcDictypeEntity cdcDictypeEntity = new CdcDictypeEntity();
                    cdcDictypeEntity.setCode("bus_order_instruc");
                    cdcDictypeEntity.setIsActive(1);
                    cdcDictypeEntity = cdcDictypeMapper.selectOne(cdcDictypeEntity);
                    Long dicorderinstrucId = cdcDictypeEntity.getId();
                    //如果没有对应ID就要插入
                    CdcDictionaryEntity cdcDictionaryEntity = new CdcDictionaryEntity();
                    cdcDictionaryEntity.setDictypeId(dicorderinstrucId);
                    cdcDictionaryEntity.setCode(busOrderInstrucCode);
                    cdcDictionaryEntity.setIsActive(1);
                    cdcDictionaryEntity = cdcDictionaryMapper.selectOne(cdcDictionaryEntity);
                    //判断字典存在不存在
                    Long busorderinstrucid = null;
                    if (CommonUtil.isEmpty(cdcDictionaryEntity)) {//不存在  保存到字典表里面
                        cdcDictionaryEntity = new CdcDictionaryEntity();
                        cdcDictionaryEntity.setDictypeId(dicorderinstrucId);
                        cdcDictionaryEntity.setCode(busOrderInstrucCode);
                        cdcDictionaryEntity.setDescription(busOrderInstrucDesc);
                        cdcDictionaryEntity.setIsActive(1);
                        //保存字典
                        cdcDictionaryMapper.insert(cdcDictionaryEntity);
                        busorderinstrucid = cdcDictionaryEntity.getId();
                    } else {
                        busorderinstrucid = cdcDictionaryEntity.getId();
                    }
                    order.setBusOrderInstrucId(busorderinstrucid);
                }

                if (CommonUtil.isNotEmpty(doseQty)) {
                    Float doseqtyf = Float.valueOf(doseQty);
                    order.setDoseQty(doseqtyf);
                }
                //orderpriorityid
                if (CommonUtil.isNotEmpty(busOrderPriorityCode)) {
                    CdcDictypeEntity cdcDictypeEntity = new CdcDictypeEntity();
                    cdcDictypeEntity.setCode("bus_order_priority");
                    cdcDictypeEntity.setIsActive(1);
                    cdcDictypeEntity = cdcDictypeMapper.selectOne(cdcDictypeEntity);
                    Long dicorderpriorityId = cdcDictypeEntity.getId();
                    //如果没有对应ID就要插入
                    CdcDictionaryEntity cdcDictionaryEntity = new CdcDictionaryEntity();
                    cdcDictionaryEntity.setDictypeId(dicorderpriorityId);
                    cdcDictionaryEntity.setCode(busOrderPriorityCode);
                    cdcDictionaryEntity.setIsActive(1);
                    cdcDictionaryEntity = cdcDictionaryMapper.selectOne(cdcDictionaryEntity);
                    //判断字典存在不存在
                    Long busorderpriorityid = null;
                    if (CommonUtil.isEmpty(cdcDictionaryEntity)) {//不存在  保存到字典表里面
                        cdcDictionaryEntity = new CdcDictionaryEntity();
                        cdcDictionaryEntity.setDictypeId(dicorderpriorityId);
                        cdcDictionaryEntity.setCode(busOrderPriorityCode);
                        cdcDictionaryEntity.setDescription(busOrderPriorityDesc);
                        cdcDictionaryEntity.setIsActive(1);
                        //保存字典
                        cdcDictionaryMapper.insert(cdcDictionaryEntity);
                        busorderpriorityid = cdcDictionaryEntity.getId();
                    } else {
                        busorderpriorityid = cdcDictionaryEntity.getId();
                    }
                    order.setBusOrderPriorityId(busorderpriorityid);
                }
                //orderunitid
                if (CommonUtil.isNotEmpty(busOrderUnitCode)) {
                    CdcDictypeEntity cdcDictypeEntity = new CdcDictypeEntity();
                    cdcDictypeEntity.setCode("bus_order_unit");
                    cdcDictypeEntity.setIsActive(1);
                    cdcDictypeEntity = cdcDictypeMapper.selectOne(cdcDictypeEntity);
                    Long dicorderunitId = cdcDictypeEntity.getId();
                    //如果没有对应ID就要插入
                    CdcDictionaryEntity cdcDictionaryEntity = new CdcDictionaryEntity();
                    cdcDictionaryEntity.setDictypeId(dicorderunitId);
                    cdcDictionaryEntity.setCode(busOrderUnitCode);
                    cdcDictionaryEntity.setIsActive(1);
                    cdcDictionaryEntity = cdcDictionaryMapper.selectOne(cdcDictionaryEntity);
                    //判断字典存在不存在
                    Long busorderunitid = null;
                    if (CommonUtil.isEmpty(cdcDictionaryEntity)) {//不存在  保存到字典表里面
                        cdcDictionaryEntity = new CdcDictionaryEntity();
                        cdcDictionaryEntity.setDictypeId(dicorderunitId);
                        cdcDictionaryEntity.setCode(busOrderUnitCode);
                        cdcDictionaryEntity.setDescription(busOrderUnitDesc);
                        cdcDictionaryEntity.setIsActive(1);
                        //保存字典
                        cdcDictionaryMapper.insert(cdcDictionaryEntity);
                        busorderunitid = cdcDictionaryEntity.getId();
                    } else {
                        busorderunitid = cdcDictionaryEntity.getId();
                    }
                    order.setBusOrderUnitId(busorderunitid);
                }
                //orderfreqid
                if (CommonUtil.isNotEmpty(busOrderFreqCode)) {
                    CdcDictypeEntity cdcDictypeEntity = new CdcDictypeEntity();
                    cdcDictypeEntity.setCode("bus_order_freq");
                    cdcDictypeEntity.setIsActive(1);
                    cdcDictypeEntity = cdcDictypeMapper.selectOne(cdcDictypeEntity);
                    Long dicorderfreqId = cdcDictypeEntity.getId();
                    //如果没有对应ID就要插入
                    CdcDictionaryEntity cdcDictionaryEntity = new CdcDictionaryEntity();
                    cdcDictionaryEntity.setDictypeId(dicorderfreqId);
                    cdcDictionaryEntity.setCode(busOrderFreqCode);
                    cdcDictionaryEntity.setIsActive(1);
                    cdcDictionaryEntity = cdcDictionaryMapper.selectOne(cdcDictionaryEntity);
                    //判断字典存在不存在
                    Long busorderfreqid = null;
                    if (CommonUtil.isEmpty(cdcDictionaryEntity)) {//不存在  保存到字典表里面
                        cdcDictionaryEntity = new CdcDictionaryEntity();
                        cdcDictionaryEntity.setDictypeId(dicorderfreqId);
                        cdcDictionaryEntity.setCode(busOrderFreqCode);
                        cdcDictionaryEntity.setDescription(busOrderFreqDesc);
                        cdcDictionaryEntity.setIsActive(1);
                        //保存字典
                        cdcDictionaryMapper.insert(cdcDictionaryEntity);
                        busorderfreqid = cdcDictionaryEntity.getId();
                    } else {
                        busorderfreqid = cdcDictionaryEntity.getId();
                    }
                    order.setBusOrderFreqId(busorderfreqid);
                }
                if (CommonUtil.isNotEmpty(isAnti)) {
                    Integer isAntif = Integer.valueOf(isAnti);
                    //order.setIsAnti(isAntif);
                }
                order.setDataId(dataId);
                order.setIsActive(1);

                //先判断是不是有该对象
                Wrapper<CdcOrdexecEntity> wrapper1 = new EntityWrapper<CdcOrdexecEntity>();
                wrapper1.eq("xcode", order.getXcode());
                wrapper1.eq("data_id", dataId);
                int count = cdcOrdexecService.selectCount(wrapper1);
                if (count > 0) {
                    cdcOrdexecService.update(order, wrapper1);
                } else {
                    cdcOrdexecService.insert(order);
                }
            }
        //发送消息推送给前端刷新页面
        MyWebSocketHandler.fanoutMessage(episodeId);

    }

    @Override
    public void handleTransferInfo(String dataStr) throws Exception{

            JSONObject dataJObj = JSONObject.parseObject(dataStr);

            String episodeId = dataJObj.getString("episode_id"); // 就诊记录
            String xcode = dataJObj.getString("xcode"); // 索引码
            String tranLocCode = dataJObj.getString("tran_loc_code"); // 转入科室代码
            String fromLocCode = dataJObj.getString("from_loc_code"); // 转出科室代码
            String tranDate = dataJObj.getString("tran_date"); // 转入日期
            String fromDate = dataJObj.getString("from_date"); // 转出日期
            String inLocDays = dataJObj.getString("in_loc_days"); // 在科天数
//            String transferResult = dataJObj.getString("transfer_result"); // 转科说明

            // 根据转科信息进行处理或存储
            //获取episodeid
            Wrapper<CdcEpisodeEntity> wrapper = new EntityWrapper<CdcEpisodeEntity>();
            wrapper.eq("xcode", episodeId);
            CdcEpisodeEntity cdcEpisodeEntity = cdcEpisodeService.selectOne(wrapper);//就诊信息没在库中？？？
            if (CommonUtil.isEmpty(cdcEpisodeEntity)) {
                return;
            }
            Long epsid = cdcEpisodeEntity.getId();
            //获取basedataId
            CdcDataEntity cdcDataEntityBase = new CdcDataEntity();
            cdcDataEntityBase.setCode("HisBase");
            cdcDataEntityBase.setIsActive(1);
            cdcDataEntityBase = cdcDataMapper.selectOne(cdcDataEntityBase);
            Long basedataId = cdcDataEntityBase.getId();

            CdcTransferEntity transfer = new CdcTransferEntity();
            transfer.setEpisodeId(epsid);
            transfer.setXcode(xcode);
            if (CommonUtil.isNotEmpty(tranLocCode)) {
                CdcLocationEntity cdcLocationEntity = new CdcLocationEntity();
                cdcLocationEntity.setXcode(tranLocCode);
                cdcLocationEntity.setDataId(basedataId);
                cdcLocationEntity.setIsActive(1);
                cdcLocationEntity = cdcLocationMapper.selectOne(cdcLocationEntity);
                if (CommonUtil.isNotEmpty(cdcLocationEntity)) {
                    Long tranLocId = cdcLocationEntity.getId();
                    transfer.setTranLocId(tranLocId);
                }
            }
            if (CommonUtil.isNotEmpty(fromLocCode)) {
                CdcLocationEntity cdcLocationEntity = new CdcLocationEntity();
                cdcLocationEntity.setXcode(fromLocCode);
                cdcLocationEntity.setDataId(basedataId);
                cdcLocationEntity.setIsActive(1);
                cdcLocationEntity = cdcLocationMapper.selectOne(cdcLocationEntity);
                if (CommonUtil.isNotEmpty(cdcLocationEntity)) {
                    Long fromLocId = cdcLocationEntity.getId();
                    transfer.setFromLocId(fromLocId);
                }
            }
            if (CommonUtil.isNotEmpty(tranDate)) {
                Date trandatef = getTime(tranDate);
                transfer.setTranDate(trandatef);
            }
            if (CommonUtil.isNotEmpty(fromDate)) {
                Date fromDatef = getTime(fromDate);
                transfer.setTranDate(fromDatef);
            }
            if (CommonUtil.isNotEmpty(inLocDays)) {
                transfer.setInLocDays(Integer.valueOf(inLocDays));
            }
            //先判断是不是有该对象
            Wrapper<CdcTransferEntity> wrapper1 = new EntityWrapper<CdcTransferEntity>();
            wrapper1.eq("episode_id", transfer.getEpisodeId());
            wrapper1.eq("xcode", transfer.getXcode());
            int count = cdcTransferService.selectCount(wrapper1);
            if (count > 0) {
                cdcTransferService.update(transfer, wrapper1);
            } else {
                cdcTransferService.insert(transfer);
            }

    }

    @Override
    public void handleEmrInfo(String dataStr) throws Exception{
            String nlpUrl = CdcConfigCache.me().getValueByKey("SDS", "SDAnalysisServer");
            JSONObject dataJObj = JSONObject.parseObject(dataStr);
            String episodeId = dataJObj.getString("episode_id"); // 就诊记录
            //获取episodeid
            Wrapper<CdcEpisodeEntity> wrapper = new EntityWrapper<CdcEpisodeEntity>();
            wrapper.eq("xcode", episodeId);
            CdcEpisodeEntity cdcEpisodeEntity = cdcEpisodeService.selectOne(wrapper);//就诊信息没在库中？？？
            if (CommonUtil.isEmpty(cdcEpisodeEntity)) {
                return;
            }
            Long epsid = cdcEpisodeEntity.getId();
            //获取basedataId
            CdcDataEntity cdcDataEntityBase = new CdcDataEntity();
            cdcDataEntityBase.setCode("HisBase");
            cdcDataEntityBase.setIsActive(1);
//            cdcDataEntityBase = cdcDataMapper.selectOne(cdcDataEntityBase);
            JSONArray progressNoteList = dataJObj.getJSONArray("ProgressNoteList");
            //获取dataid
            CdcDataEntity cdcDataEntity = new CdcDataEntity();
            cdcDataEntity.setCode("EmrData");
            cdcDataEntity.setIsActive(1);
            cdcDataEntity = cdcDataMapper.selectOne(cdcDataEntity);
            Long dataId = cdcDataEntity.getId();
            List<CdcEmrEntity> emrList = new ArrayList<>();
            for (int i = 0; i < progressNoteList.size(); i++) {
                JSONObject progressNoteObj = progressNoteList.getJSONObject(i);
                String xcode = progressNoteObj.getString("xcode"); // 索引码
                String busEmrTypeCode = progressNoteObj.getString("bus_emr_type_code"); // 文档类型代码
                String title = progressNoteObj.getString("title"); // 标题
                String subCode = progressNoteObj.getString("sub_code"); // 子标题代码
                String subTitle = progressNoteObj.getString("sub_title"); // 子标题
                String content = progressNoteObj.getString("content"); // 内容
                String emrDate = progressNoteObj.getString("emr_date"); // 填写日期
                String emrUserId = progressNoteObj.getString("emr_user_id"); // 填写人id
                String updateTime = progressNoteObj.getString("update_time"); // 更新时间
//                String updateUserId = progressNoteObj.getString("update_user_id"); // 更新人id

                // 根据病历信息进行处理或存储
                CdcEmrEntity emr = new CdcEmrEntity();
                emr.setXcode(xcode);
                emr.setEpisodeId(epsid);
                //获取emrtypeId
                if (CommonUtil.isNotEmpty(busEmrTypeCode)) {
                    CdcDictypeEntity cdcDictypeEntity = new CdcDictypeEntity();
                    cdcDictypeEntity.setCode("bus_emr_type");
                    cdcDictypeEntity.setIsActive(1);
                    cdcDictypeEntity = cdcDictypeMapper.selectOne(cdcDictypeEntity);
                    Long dicemrtypeId = cdcDictypeEntity.getId();
                    //如果没有对应ID就要插入
                    CdcDictionaryEntity cdcDictionaryEntity = new CdcDictionaryEntity();
                    cdcDictionaryEntity.setDictypeId(dicemrtypeId);
                    cdcDictionaryEntity.setCode(busEmrTypeCode);
                    cdcDictionaryEntity.setIsActive(1);
                    cdcDictionaryEntity = cdcDictionaryMapper.selectOne(cdcDictionaryEntity);
                    //判断字典存在不存在
                    Long busemrtypeid = null;
                    if (CommonUtil.isEmpty(cdcDictionaryEntity)) {//不存在  保存到字典表里面
                        cdcDictionaryEntity = new CdcDictionaryEntity();
                        cdcDictionaryEntity.setDictypeId(dicemrtypeId);
                        cdcDictionaryEntity.setCode(busEmrTypeCode);
                        cdcDictionaryEntity.setDescription(title);
                        cdcDictionaryEntity.setIsActive(1);
                        //保存字典
                        cdcDictionaryMapper.insert(cdcDictionaryEntity);
                        busemrtypeid = cdcDictionaryEntity.getId();
                    } else {
                        busemrtypeid = cdcDictionaryEntity.getId();
                    }
                    emr.setBusEmrTypeId(busemrtypeid);
                }
                if (CommonUtil.isNotEmpty(title)) {
                    emr.setTitle(title);
                }
                if (CommonUtil.isNotEmpty(subCode)) {
                    emr.setSubCode(subCode);
                }
                if (CommonUtil.isNotEmpty(subTitle)) {
                    emr.setSubTitle(subTitle);
                }
                if (CommonUtil.isNotEmpty(content)) {
                    emr.setContent(content);
                }
                if (CommonUtil.isNotEmpty(emrDate)) {
                    Date emrdatef = getTime(emrDate);
                    emr.setEmrDate(emrdatef);
                }
                //获取填写人
                if (CommonUtil.isNotEmpty(emrUserId)) {
                    emr.setEmrUserId(Long.valueOf(emrUserId));
                }
                if (CommonUtil.isNotEmpty(updateTime)) {
                    Date emrUpdateDate = getTime(updateTime);
                    emr.setUpdateDate(emrUpdateDate);
                }
                emr.setDataId(dataId);
                emr.setIsActive(1);

                //先判断是不是有该对象
                Wrapper<CdcEmrEntity> emrWrapper = new EntityWrapper<CdcEmrEntity>();
                emrWrapper.eq("xcode", emr.getXcode());
                emrWrapper.eq("sub_code", emr.getSubCode());
                emrWrapper.eq("data_id", dataId);
                int count = cdcEmrService.selectCount(emrWrapper);
                boolean result;
                if (count > 0) {
                    result = cdcEmrService.update(emr, emrWrapper);
                } else {
                    result = cdcEmrService.insert(emr);
                }
                if (result) {
                    emrList.add(emr);
                }
            }
            //解析病历并存储
            cdcBsEmrStructService.emrStructByNLP(nlpUrl, emrList);
            //发送消息推送给前端刷新页面
            MyWebSocketHandler.fanoutMessage(episodeId);
    }

    @Override
    public void handleLabVisitInfo(String dataStr) throws Exception{

            JSONObject dataJObj = JSONObject.parseObject(dataStr);
            String episodeId = dataJObj.getString("episode_id"); // 就诊记录
            //获取episodeid
            Wrapper<CdcEpisodeEntity> wrapper = new EntityWrapper<CdcEpisodeEntity>();
            wrapper.eq("xcode", episodeId);
            CdcEpisodeEntity cdcEpisodeEntity = cdcEpisodeService.selectOne(wrapper);//就诊信息没在库中？？？
            if (CommonUtil.isEmpty(cdcEpisodeEntity)) {
                return;
            }
            Long epsid = cdcEpisodeEntity.getId();
            //获取basedataId
            CdcDataEntity cdcDataEntityBase = new CdcDataEntity();
            cdcDataEntityBase.setCode("HisBase");
            cdcDataEntityBase.setIsActive(1);
            cdcDataEntityBase = cdcDataMapper.selectOne(cdcDataEntityBase);
            Long basedataId = cdcDataEntityBase.getId();
            JSONArray progressNoteList = dataJObj.getJSONArray("ProgressNoteList");
            //获取dataid
            CdcDataEntity cdcDataEntity = new CdcDataEntity();
            cdcDataEntity.setCode("LabData");
            cdcDataEntity.setIsActive(1);
            cdcDataEntity = cdcDataMapper.selectOne(cdcDataEntity);
            Long dataId = cdcDataEntity.getId();

            JSONArray labVisitList = dataJObj.getJSONArray("LabVisitList");
            for (int i = 0; i < labVisitList.size(); i++) {
                JSONObject labVisitObj = labVisitList.getJSONObject(i);
                String orderExecId = labVisitObj.getString("order_exec_id"); // 医嘱记录id
                String xcode = labVisitObj.getString("xcode"); // 索引码
                String specVisitNum = labVisitObj.getString("spec_visit_num"); // 检验号|标本号
                String specimen = labVisitObj.getString("specimen"); // 标本
                String labLocCode = labVisitObj.getString("lab_loc_code"); // 申请科室|送检科室代码
                String labWardCode = labVisitObj.getString("lab_ward_code"); // 申请病区|送检病区代码
                String colDate = labVisitObj.getString("col_date"); // 采集日期
                String colUserName = labVisitObj.getString("col_user_name"); // 采集人
                String recDate = labVisitObj.getString("rec_date"); // 接收日期
                String recUserName = labVisitObj.getString("rec_user_name"); // 接受人

                // 根据送检记录信息进行处理或存储
                CdcLabVisitEntity labVist = new CdcLabVisitEntity();
                labVist.setEpisodeId(epsid);
                labVist.setXcode(xcode);
                if (CommonUtil.isNotEmpty(specVisitNum)) {
                    labVist.setSpecVisitNum(specVisitNum);
                }
                if (CommonUtil.isNotEmpty(specimen)) {
                    labVist.setSpecimen(specimen);
                }
                if (CommonUtil.isNotEmpty(labLocCode)) {
                    CdcLocationEntity cdcLocationEntity = new CdcLocationEntity();
                    cdcLocationEntity.setXcode(labLocCode);
                    cdcLocationEntity.setDataId(basedataId);
                    cdcLocationEntity.setIsActive(1);
                    cdcLocationEntity = cdcLocationMapper.selectOne(cdcLocationEntity);
                    if (CommonUtil.isNotEmpty(cdcLocationEntity)) {
                        Long labloclongid = cdcLocationEntity.getId();
                        labVist.setLabLocId(labloclongid);
                    }
                }
                if (CommonUtil.isNotEmpty(labWardCode)) {
                    CdcLocationEntity cdcLocationEntity = new CdcLocationEntity();
                    cdcLocationEntity.setXcode(labWardCode);
                    cdcLocationEntity.setDataId(basedataId);
                    cdcLocationEntity.setIsActive(1);
                    cdcLocationEntity = cdcLocationMapper.selectOne(cdcLocationEntity);
                    if (CommonUtil.isNotEmpty(cdcLocationEntity)) {
                        Long labwardlongid = cdcLocationEntity.getId();
                        labVist.setLabWardId(labwardlongid);
                    }
                }
                if (CommonUtil.isNotEmpty(colDate)) {
                    Date coldatef = getTime(colDate);
                    labVist.setColDate(coldatef);
                }
                if (CommonUtil.isNotEmpty(colUserName)) {
                    labVist.setColUserName(colUserName);
                }
                if (CommonUtil.isNotEmpty(recDate)) {
                    Date recdatef = getTime(recDate);
                    labVist.setRecDate(recdatef);
                }
                if (CommonUtil.isNotEmpty(recUserName)) {
                    labVist.setRecUserName(recUserName);
                }
                labVist.setDataId(dataId);
                labVist.setIsActive(1);
                //先判断是不是有该对象
                Wrapper<CdcLabVisitEntity> wrapper1 = new EntityWrapper<CdcLabVisitEntity>();
                wrapper1.eq("xcode", labVist.getXcode());
                wrapper1.eq("data_id", dataId);
                CdcLabVisitEntity old = cdcLabVisitService.selectOne(wrapper1);
                Long visitId = null;
                if (CommonUtil.isNotEmpty(old)) {
                    cdcLabVisitService.update(labVist, wrapper1);
                    visitId = old.getId();
                } else {
                    cdcLabVisitService.insert(labVist);
                    visitId = labVist.getId();
                }

                // 保存检验组套
                if (CommonUtil.isNotEmpty(orderExecId)) {
                    CdcLabSetEntity labSet = new CdcLabSetEntity();
                    CdcOrdexecEntity ordexec = new CdcOrdexecEntity();
                    ordexec.setXcode(orderExecId);
                    ordexec = cdcOrdexecMapper.selectOne(ordexec);
                    if (CommonUtil.isNotEmpty(ordexec)) {
                        labSet.setOrderExecId(ordexec.getId());
                    } else {
                        continue;
                    }
                    // 获取检验医嘱
                    CdcLabOrderEntity labOrder = new CdcLabOrderEntity();
                    labOrder.setOrderMastId(ordexec.getOrderMastId());
                    labOrder = cdcLabOrderMapper.selectOne(labOrder);
                    if (CommonUtil.isNotEmpty(labOrder)) {
                        labSet.setCtLabOrderId(labOrder.getId());
                    }
                    labSet.setLabVisitId(visitId);
                    cdcLabSetService.insert(labSet);
                }
            }
    }

    @Override
    public void handleLabReportInfo(String dataStr) throws Exception{

            JSONObject dataJObj = JSONObject.parseObject(dataStr);
            String episodeId = dataJObj.getString("episode_id"); // 就诊记录
            //获取episodeid
            Wrapper<CdcEpisodeEntity> wrapper = new EntityWrapper<CdcEpisodeEntity>();
            wrapper.eq("xcode", episodeId);
            CdcEpisodeEntity cdcEpisodeEntity = cdcEpisodeService.selectOne(wrapper);//就诊信息没在库中？？？
            if (CommonUtil.isEmpty(cdcEpisodeEntity)) {
                return;
            }
            Long epsid = cdcEpisodeEntity.getId();
            //获取labdataid
            CdcDataEntity cdcDataEntity = new CdcDataEntity();
            cdcDataEntity.setCode("LabData");
            cdcDataEntity.setIsActive(1);
            cdcDataEntity = cdcDataMapper.selectOne(cdcDataEntity);
            Long dataId = cdcDataEntity.getId();
            //获取basedataId
            CdcDataEntity cdcDataEntityBase = new CdcDataEntity();
            cdcDataEntityBase.setCode("HisBase");
            cdcDataEntityBase.setIsActive(1);
            cdcDataEntityBase = cdcDataMapper.selectOne(cdcDataEntityBase);
            Long basedataId = cdcDataEntityBase.getId();

            JSONArray labReportList = dataJObj.getJSONArray("LabReportList");
            for (int i = 0; i < labReportList.size(); i++) {
                JSONObject labReportObj = labReportList.getJSONObject(i);
                String xcode = labReportObj.getString("xcode"); // 索引码
                String labVisitNum = labReportObj.getString("lab_visit_num"); // 标本号
                String reportId = labReportObj.getString("report_id"); // 报告ID
                String reportNum = labReportObj.getString("report_num"); // 序号
                String inspectionCode = labReportObj.getString("InspectionCode"); // 检验大项编码
                String inspectionName = labReportObj.getString("InspectionName"); // 检验大项名称
                String result = labReportObj.getString("result"); // 结果
                String reportStatusCode = labReportObj.getString("bus_lab_report_status_code"); // 状态代码
                String reportStatusDesc = labReportObj.getString("bus_lab_report_status_desc"); // 状态描述
                String reportDate = labReportObj.getString("report_date"); // 录入日期
                String reportUserName = labReportObj.getString("report_user_name"); // 录入人
                String checkDate = labReportObj.getString("check_date"); // 审核日期
                String checkUserName = labReportObj.getString("check_user_name"); // 审核人
                // 根据检验报告信息进行处理或存储
                CdcLabReportEntity labrep = new CdcLabReportEntity();
                labrep.setEpisodeId(epsid);
                labrep.setXcode(xcode);
                // 获取检验样本表中的ID
                Wrapper<CdcLabVisitEntity> visitWpr = new EntityWrapper<>();
                visitWpr.eq("episode_id", episodeId).eq("xcode", labVisitNum);
                CdcLabVisitEntity cdcLabVisitEntity = cdcLabVisitService.selectOne(visitWpr);// 样本没在库中？？？
                if (CommonUtil.isEmpty(cdcLabVisitEntity)) {
                    continue;
                }
                Long labvisitlongid = cdcLabVisitEntity.getId();
                labrep.setLabVisitId(labvisitlongid);
                if (CommonUtil.isNotEmpty(reportId)) {
                    labrep.setReportId(reportId);
                }
                if (CommonUtil.isNotEmpty(reportNum)) {
                    labrep.setReportNum(reportNum);
                }
                if (CommonUtil.isNotEmpty(result)) {
                    labrep.setResult(result);
                }
                // 获取ID
                if (CommonUtil.isNotEmpty(reportStatusCode)) {
                    CdcDictypeEntity cdcDictypeEntity = new CdcDictypeEntity();
                    cdcDictypeEntity.setCode("bus_lab_status");
                    cdcDictypeEntity.setIsActive(1);
                    cdcDictypeEntity = cdcDictypeMapper.selectOne(cdcDictypeEntity);
                    Long diclabstatusId = cdcDictypeEntity.getId();
                    // 如果没有对应ID就要插入
                    CdcDictionaryEntity cdcDictionaryEntity = new CdcDictionaryEntity();
                    cdcDictionaryEntity.setDictypeId(diclabstatusId);
                    cdcDictionaryEntity.setCode(reportStatusCode);
                    cdcDictionaryEntity.setIsActive(1);
                    cdcDictionaryEntity = cdcDictionaryMapper.selectOne(cdcDictionaryEntity);
                    // 判断字典存在不存在
                    Long buslabstatusid = null;
                    if (CommonUtil.isEmpty(cdcDictionaryEntity)) {// 不存在 保存到字典表里面
                        cdcDictionaryEntity = new CdcDictionaryEntity();
                        cdcDictionaryEntity.setDictypeId(diclabstatusId);
                        cdcDictionaryEntity.setCode(reportStatusCode);
                        cdcDictionaryEntity.setDescription(reportStatusDesc);
                        cdcDictionaryEntity.setIsActive(1);
                        // 保存字典
                        cdcDictionaryMapper.insert(cdcDictionaryEntity);
                        buslabstatusid = cdcDictionaryEntity.getId();
                    } else {
                        buslabstatusid = cdcDictionaryEntity.getId();
                    }
                    labrep.setBusLabReportStatusId(buslabstatusid);
                }
                if (CommonUtil.isNotEmpty(reportDate)) {
                    Date reportdatef = getTime(reportDate);
                    labrep.setReportDate(reportdatef);
                }
                if (CommonUtil.isNotEmpty(reportUserName)) {
                    labrep.setReportUserName(reportUserName);
                }
                if (CommonUtil.isNotEmpty(checkDate)) {
                    Date checkdatef = getTime(checkDate);
                    labrep.setCheckDate(checkdatef);
                }
                if (CommonUtil.isNotEmpty(checkUserName)) {
                    labrep.setCheckUserName(checkUserName);
                }
                labrep.setDataId(dataId);
                labrep.setIsActive(1);
                // 先判断是不是有该对象
                Wrapper<CdcLabReportEntity> repWpr = new EntityWrapper<CdcLabReportEntity>();
                repWpr.eq("xcode", labrep.getXcode());
                repWpr.eq("data_id", dataId);
                CdcLabReportEntity old = cdcLabReportService.selectOne(repWpr);
                Long reportLongId = null;
                if (CommonUtil.isNotEmpty(old)) {
                    cdcLabReportService.update(labrep, repWpr);
                    reportLongId = old.getId();
                } else {
                    cdcLabReportService.insert(labrep);
                    reportLongId = labrep.getId();
                }

                JSONArray labResultList = labReportObj.getJSONArray("LabResultList");
                for (int j = 0; j < labResultList.size(); j++) {
                    JSONObject labResultObj = labResultList.getJSONObject(j);
                    String labResultId = labResultObj.getString("xcode"); // 检验结果唯一码
                    String labItemCode = labResultObj.getString("lab_item_code"); // 检验小项代码
                    String labItemDesc = labResultObj.getString("Lab_item_desc"); // 检验小项描述
                    String resultValue = labResultObj.getString("result"); // 检测结果
                    String resultType = labResultObj.getString("result_type"); // 定性结果
                    String resultNote = labResultObj.getString("result_note"); // 结果说明
                    String abnormalDesc = labResultObj.getString("abnormal_desc"); // 异常提示
                    String resultUnit = labResultObj.getString("result_unit"); // 结果单位
                    String resultRange = labResultObj.getString("result_range"); // 参考范围
                    String resultMdr = labResultObj.getString("result_mdr"); // 多耐结果

                    // 根据检验结果信息进行处理或存储
                    //1、更新检验项目字典
                    CdcLabItemEntity labItem = new CdcLabItemEntity();
                    labItem.setXcode(labItemCode);
                    labItem.setCode(labItemCode);
                    labItem.setDescription(labItemDesc);

                    Wrapper<CdcLabItemEntity> labItemWpr = new EntityWrapper<>();
                    labItemWpr.eq("xcode", labItemCode).eq("data_id", dataId);
                    CdcLabItemEntity oldItem = cdcLabItemService.selectOne(labItemWpr);
                    Long labItemLongId = null;
                    if (CommonUtil.isNotEmpty(oldItem)) {
                        labItem.setCode(labItemCode);
                        labItem.setDescription(labItemDesc);
                        labItem.updateById();
                        cdcLabItemService.update(labItem, labItemWpr);
                        labItemLongId = oldItem.getId();
                    } else {
                        cdcLabItemService.insert(labItem);
                        labItemLongId = labItem.getId();
                    }
                    //2、更新检验医嘱和检验项目的关联关系
                    //通过标本号查询医嘱记录
                    Wrapper<CdcLabSetEntity> labSetWpr = new EntityWrapper<>();
                    labSetWpr.eq("lab_visit_id", labvisitlongid);
                    List<CdcLabSetEntity> labSetList = cdcLabSetService.selectList(labSetWpr);
                    for (CdcLabSetEntity labSet : labSetList) {
                        Long labOrderId = labSet.getCtLabOrderId();
                        CdcLabEntryEntity labEntry = new CdcLabEntryEntity();
                        labEntry.setLabItemId(labItemLongId);
                        labEntry.setLabOrderId(labOrderId);
                        Wrapper<CdcLabEntryEntity> entryWpr = new EntityWrapper<>();
                        entryWpr.eq("lab_item_id", labItemLongId).eq("lab_order_id", labOrderId);
                        int count = cdcLabEntryMapper.selectCount(entryWpr);
                        if (count == 0) {
                            cdcLabEntryMapper.insert(labEntry);
                        }
                    }
                    //3、保存检验结果
                    CdcLabResultEntity labRst = new CdcLabResultEntity();
                    labRst.setXcode(labResultId);
                    labRst.setLabReportId(reportLongId);
                    // 获取labreportid
                    Long labreportLongid = labrep.getId();
                    labRst.setLabReportId(labreportLongid);
                    // 获取labitemID
                    if (CommonUtil.isNotEmpty(labItemLongId)) {
                        labRst.setLabItemId(labItemLongId);
                    }
                    if (CommonUtil.isNotEmpty(resultValue)) {
                        labRst.setResult(resultValue);
                    }
                    if (CommonUtil.isNotEmpty(resultType)) {
                        labRst.setResultType(resultType);
                    }
                    if (CommonUtil.isNotEmpty(resultNote)) {
                        labRst.setResultNote(resultNote);
                    }
                    if (CommonUtil.isNotEmpty(abnormalDesc)) {
                        labRst.setAbnormalDesc(abnormalDesc);
                    }
                    if (CommonUtil.isNotEmpty(resultUnit)) {
                        labRst.setResultUnit(resultUnit);
                    }
                    if (CommonUtil.isNotEmpty(resultRange)) {
                        labRst.setResultRange(resultRange);
                    }
                    if (CommonUtil.isNotEmpty(resultMdr)) {
                        labRst.setResultMdr(resultMdr);
                    }
                    labrep.setDataId(dataId);
                    labRst.setIsActive(1);

                    // 先判断是不是有该对象
                    Wrapper<CdcLabResultEntity> itemwrapper = new EntityWrapper<CdcLabResultEntity>();
                    itemwrapper.eq("xcode", labRst.getXcode());
                    itemwrapper.eq("data_id", dataId);
                    int itemcount = cdcLabResultService.selectCount(itemwrapper);
                    if (itemcount > 0) {
                        cdcLabResultService.update(labRst, itemwrapper);
                    } else {
                        cdcLabResultService.insert(labRst);
                    }
                }

            }
        //发送消息推送给前端刷新页面
        MyWebSocketHandler.fanoutMessage(episodeId);

    }

    @Override
    public void handleExamReportInfo(String dataStr) throws Exception{
            JSONObject dataJObj = JSONObject.parseObject(dataStr);
            String episodeId = dataJObj.getString("episodeId"); // 就诊记录
            String xcode = dataJObj.getString("xcode"); // 索引码
            String ordExecId = dataJObj.getString("ordExecId"); // 医嘱记录ID
            String examNum = dataJObj.getString("examNum"); // 检查号
            String reportStatusCode = dataJObj.getString("reportStatusCode"); // 状态代码
            String reportStatusDesc = dataJObj.getString("reportStatusDesc"); // 状态描述
            String examCode = dataJObj.getString("examCode"); // 检查代码
            String examName = dataJObj.getString("examName"); // 检查名称
            String examDesc = dataJObj.getString("examDesc"); // 检查所见
            String resultDesc = dataJObj.getString("resultDesc"); // 诊断意见
            String repUserName = dataJObj.getString("repUserName"); // 报告人
            String repDate = dataJObj.getString("repDate"); // 报告日期
            String regUserName = dataJObj.getString("regUserName"); // 登记人
            String regDate = dataJObj.getString("regDate"); // 登记日期

            //获取episodeid
            Wrapper<CdcEpisodeEntity> wrapper = new EntityWrapper<CdcEpisodeEntity>();
            wrapper.eq("xcode", episodeId);
            CdcEpisodeEntity cdcEpisodeEntity = cdcEpisodeService.selectOne(wrapper);//就诊信息没在库中？？？
            if (CommonUtil.isEmpty(cdcEpisodeEntity)) {
                return;
            }
            Long epsid = cdcEpisodeEntity.getId();
            // 根据检查报告信息进行处理或存储
            CdcExaminateEntity exam = new CdcExaminateEntity();
            exam.setEpisodeId(epsid);
            exam.setXcode(xcode);
            //获取dataid
            CdcDataEntity cdcDataEntity = new CdcDataEntity();
            cdcDataEntity.setCode("EmrData");
            cdcDataEntity.setIsActive(1);
            cdcDataEntity = cdcDataMapper.selectOne(cdcDataEntity);
            Long dataId = cdcDataEntity.getId();
            //获取basedataId
            CdcDataEntity cdcDataEntityBase = new CdcDataEntity();
            cdcDataEntityBase.setCode("HisBase");
            cdcDataEntityBase.setIsActive(1);
            cdcDataEntityBase = cdcDataMapper.selectOne(cdcDataEntityBase);
            Long basedataId = cdcDataEntityBase.getId();
            //获取orddataId
            CdcDataEntity cdcDataEntityOrd = new CdcDataEntity();
            cdcDataEntityOrd.setCode("OrdData");
            cdcDataEntityOrd.setIsActive(1);
            cdcDataEntityOrd = cdcDataMapper.selectOne(cdcDataEntityOrd);
            Long orddataId = cdcDataEntityOrd.getId();
            //根据医嘱条目id获取longid
            if (CommonUtil.isNotEmpty(ordExecId)) {
                //如果没有对应ID就要插入
                CdcOrdexecEntity cdcOrdexecEntity = new CdcOrdexecEntity();
                cdcOrdexecEntity.setXcode(ordExecId);
                cdcOrdexecEntity.setDataId(orddataId);
                cdcOrdexecEntity = cdcOrdexecMapper.selectOne(cdcOrdexecEntity);
                if (CommonUtil.isNotEmpty(cdcOrdexecEntity)) {
                    Long orderid = cdcOrdexecEntity.getId();
                    exam.setBsOrdexecId(orderid);
                }
            }
            //获取examstatusId
            if (CommonUtil.isNotEmpty(reportStatusCode)) {
                CdcDictypeEntity cdcDictypeEntity = new CdcDictypeEntity();
                cdcDictypeEntity.setCode("bus_exam_status");
                cdcDictypeEntity.setIsActive(1);
                cdcDictypeEntity = cdcDictypeMapper.selectOne(cdcDictypeEntity);
                Long dicexamstatusId = cdcDictypeEntity.getId();
                //如果没有对应ID就要插入
                CdcDictionaryEntity cdcDictionaryEntity = new CdcDictionaryEntity();
                cdcDictionaryEntity.setDictypeId(dicexamstatusId);
                cdcDictionaryEntity.setCode(reportStatusCode);
                cdcDictionaryEntity.setIsActive(1);
                cdcDictionaryEntity = cdcDictionaryMapper.selectOne(cdcDictionaryEntity);
                //判断字典存在不存在
                Long busexamstatusid = null;
                if (CommonUtil.isEmpty(cdcDictionaryEntity)) {//不存在  保存到字典表里面
                    cdcDictionaryEntity = new CdcDictionaryEntity();
                    cdcDictionaryEntity.setDictypeId(dicexamstatusId);
                    cdcDictionaryEntity.setCode(reportStatusCode);
                    cdcDictionaryEntity.setDescription(reportStatusDesc);
                    cdcDictionaryEntity.setIsActive(1);
                    //保存字典
                    cdcDictionaryMapper.insert(cdcDictionaryEntity);
                    busexamstatusid = cdcDictionaryEntity.getId();
                } else {
                    busexamstatusid = cdcDictionaryEntity.getId();
                }
                exam.setBusExamReportStatusId(busexamstatusid);
            }
            if (CommonUtil.isNotEmpty(examNum)) {
                exam.setExamNum(examNum);
            }
            if (CommonUtil.isNotEmpty(examName)) {
                exam.setDescription(examName);
            }
            if (CommonUtil.isNotEmpty(examDesc)) {
                exam.setExamDesc(examDesc);
            }
            if (CommonUtil.isNotEmpty(resultDesc)) {
                exam.setResultDesc(resultDesc);
            }
            if (CommonUtil.isNotEmpty(repUserName)) {
                exam.setRepUserName(repUserName);
            }
            if (CommonUtil.isNotEmpty(repDate)) {
                Date repdatef = getTime(repDate);
                exam.setRepDate(repdatef);
            }
            if (CommonUtil.isNotEmpty(regUserName)) {
                exam.setRegUserName(regUserName);
            }
            if (CommonUtil.isNotEmpty(regDate)) {
                Date regDatef = getTime(regDate);
                exam.setRegDate(regDatef);
            }
            exam.setDataId(dataId);
            exam.setIsActive(1);
            //先判断是不是有该对象
            Wrapper<CdcExaminateEntity> examWpr = new EntityWrapper<CdcExaminateEntity>();
            examWpr.eq("xcode", exam.getXcode());
            examWpr.eq("data_id", dataId);
            int count = cdcExaminationService.selectCount(examWpr);
            if (count > 0) {
                cdcExaminationService.update(exam, examWpr);
            } else {
                cdcExaminationService.insert(exam);
            }
        //发送消息推送给前端刷新页面
        MyWebSocketHandler.fanoutMessage(episodeId);
    }

    @Override
    public void handleSurgeryInfo(String dataStr) throws Exception{

            JSONObject dataJObj = JSONObject.parseObject(dataStr);
            String episodeId = dataJObj.getString("episode_id"); // 就诊记录
            String xcode = dataJObj.getString("xcode"); // 索引码
            String description = dataJObj.getString("description"); // 描述
            String code = dataJObj.getString("code"); // 编码
            String busSurgTypeCode = dataJObj.getString("bus_surg_type_code"); // 类型代码
            String busSurgTypeDesc = dataJObj.getString("bus_surg_type_desc"); // 类型描述
            String busSurgStatusCode = dataJObj.getString("bus_surg_status_code"); // 状态代码
            String busSurgStatusDesc = dataJObj.getString("bus_surg_status_desc"); // 状态描述
            String startDate = dataJObj.getString("start_date"); // 手术开始日期
            String endDate = dataJObj.getString("end_date"); // 手术结束日期
            String surgLocCode = dataJObj.getString("surg_loc_code"); // 科室代码
            String surgDocId = dataJObj.getString("surg_doc_id"); // 主刀医生
            String assi1st = dataJObj.getString("assi_1st"); // 一助
            String assi2nd = dataJObj.getString("assi_2nd"); // 二助
            String busSurgInciCode = dataJObj.getString("bus_surg_inci_code"); // 切口等级代码
            String busSurgInciDesc = dataJObj.getString("bus_surg_inci_desc"); // 切口等级描述
            String busSurgHealCode = dataJObj.getString("bus_surg_heal_code"); // 愈合情况代码
            String busSurgHealDesc = dataJObj.getString("bus_surg_heal_desc"); // 愈合情况描述
            String busSurgAnesCode = dataJObj.getString("bus_surg_anes_code"); // 麻醉方式代码
            String busSurgAnesDesc = dataJObj.getString("bus_surg_anes_desc"); // 麻醉方式描述
            String anesDoc = dataJObj.getString("anes_doc"); // 麻醉医生
            String asaScore = dataJObj.getString("asa_score"); // ASA评分
            String nnisGrade = dataJObj.getString("nnis_grade"); // NNIS分级
            String wbcCount = dataJObj.getString("wbc_count"); // 术前外周WBC计数
            String inciCount = dataJObj.getString("inci_count"); // 切口个数
            String isEndoscope = dataJObj.getString("is_endoscope"); // 是否使用窥镜
            String isImplants = dataJObj.getString("is_implants"); // 是否有植入物
            String bloodLose = dataJObj.getString("blood_lose"); // 失血量
            String bloodGet = dataJObj.getString("blood_get"); // 输血量
            String complication = dataJObj.getString("complication"); // 术后并发症

            //获取episodeid
            Wrapper<CdcEpisodeEntity> wrapper = new EntityWrapper<CdcEpisodeEntity>();
            wrapper.eq("xcode", episodeId);
            CdcEpisodeEntity cdcEpisodeEntity = cdcEpisodeService.selectOne(wrapper);//就诊信息没在库中？？？
            if (CommonUtil.isEmpty(cdcEpisodeEntity)) {
                return;
            }
            Long epsid = cdcEpisodeEntity.getId();
            //获取dataid
            CdcDataEntity cdcDataEntity = new CdcDataEntity();
            cdcDataEntity.setCode("SurgeryData");
            cdcDataEntity.setIsActive(1);
            cdcDataEntity = cdcDataMapper.selectOne(cdcDataEntity);
            Long dataId = cdcDataEntity.getId();
            //获取basedataId
            CdcDataEntity cdcDataEntityBase = new CdcDataEntity();
            cdcDataEntityBase.setCode("HisBase");
            cdcDataEntityBase.setIsActive(1);
            cdcDataEntityBase = cdcDataMapper.selectOne(cdcDataEntityBase);
            Long basedataId = cdcDataEntityBase.getId();


            // 根据手术记录信息进行处理或存储
            CdcSurgeryEntity sur = new CdcSurgeryEntity();
            sur.setEpisodeId(epsid);
            sur.setXcode(xcode);
            if (CommonUtil.isNotEmpty(description)) {
                sur.setDescription(description);
            }
            if (CommonUtil.isNotEmpty(code)) {
                sur.setCode(code);
            }
            //获取surgtypeId
            if (CommonUtil.isNotEmpty(busSurgTypeCode)) {
                CdcDictypeEntity cdcDictypeEntity = new CdcDictypeEntity();
                cdcDictypeEntity.setCode("bus_surg_type");
                cdcDictypeEntity.setIsActive(1);
                cdcDictypeEntity = cdcDictypeMapper.selectOne(cdcDictypeEntity);
                Long dicsurgtypeId = cdcDictypeEntity.getId();
                //如果没有对应ID就要插入
                CdcDictionaryEntity cdcDictionaryEntity = new CdcDictionaryEntity();
                cdcDictionaryEntity.setDictypeId(dicsurgtypeId);
                cdcDictionaryEntity.setCode(busSurgTypeCode);
                cdcDictionaryEntity.setIsActive(1);
                cdcDictionaryEntity = cdcDictionaryMapper.selectOne(cdcDictionaryEntity);
                //判断字典存在不存在
                Long busdiagcateid = null;
                if (CommonUtil.isEmpty(cdcDictionaryEntity)) {//不存在  保存到字典表里面
                    cdcDictionaryEntity = new CdcDictionaryEntity();
                    cdcDictionaryEntity.setDictypeId(dicsurgtypeId);
                    cdcDictionaryEntity.setCode(busSurgTypeCode);
                    cdcDictionaryEntity.setDescription(busSurgTypeDesc);
                    cdcDictionaryEntity.setIsActive(1);
                    //保存字典
                    cdcDictionaryMapper.insert(cdcDictionaryEntity);
                    busdiagcateid = cdcDictionaryEntity.getId();
                } else {
                    busdiagcateid = cdcDictionaryEntity.getId();
                }
                sur.setBusSurgTypeId(busdiagcateid);
            }
            //获取DicSurgStatusId
            if (CommonUtil.isNotEmpty(busSurgStatusCode)) {
                CdcDictypeEntity cdcDictypeEntity = new CdcDictypeEntity();
                cdcDictypeEntity.setCode("bus_surg_status");
                cdcDictypeEntity.setIsActive(1);
                cdcDictypeEntity = cdcDictypeMapper.selectOne(cdcDictypeEntity);
                Long dicsurgstatusId = cdcDictypeEntity.getId();
                //如果没有对应ID就要插入
                CdcDictionaryEntity cdcDictionaryEntity = new CdcDictionaryEntity();
                cdcDictionaryEntity.setDictypeId(dicsurgstatusId);
                cdcDictionaryEntity.setCode(busSurgStatusCode);
                cdcDictionaryEntity.setIsActive(1);
                cdcDictionaryEntity = cdcDictionaryMapper.selectOne(cdcDictionaryEntity);
                //判断字典存在不存在
                Long bussurgstatusid = null;
                if (CommonUtil.isEmpty(cdcDictionaryEntity)) {//不存在  保存到字典表里面
                    cdcDictionaryEntity = new CdcDictionaryEntity();
                    cdcDictionaryEntity.setDictypeId(dicsurgstatusId);
                    cdcDictionaryEntity.setCode(busSurgStatusCode);
                    cdcDictionaryEntity.setDescription(busSurgStatusDesc);
                    cdcDictionaryEntity.setIsActive(1);
                    //保存字典
                    cdcDictionaryMapper.insert(cdcDictionaryEntity);
                    bussurgstatusid = cdcDictionaryEntity.getId();
                } else {
                    bussurgstatusid = cdcDictionaryEntity.getId();
                }
                sur.setBusSurgStatusId(bussurgstatusid);
            }
            if (CommonUtil.isNotEmpty(startDate)) {
                Date startdatef = getTime(startDate);
                sur.setStartDate(startdatef);
            }
            if (CommonUtil.isNotEmpty(endDate)) {
                Date enddatef = getTime(endDate);
                sur.setEndDate(enddatef);
            }
            if (CommonUtil.isNotEmpty(surgLocCode)) {
                CdcLocationEntity cdcLocationEntity = new CdcLocationEntity();
                cdcLocationEntity.setXcode(surgLocCode);
                cdcLocationEntity.setDataId(basedataId);
                cdcLocationEntity.setIsActive(1);
                cdcLocationEntity = cdcLocationMapper.selectOne(cdcLocationEntity);
                if (CommonUtil.isNotEmpty(cdcLocationEntity)) {
                    Long surgloclongid = cdcLocationEntity.getId();
                    sur.setSurgLocId(surgloclongid);
                }
            }
            //获取术者
            if (CommonUtil.isNotEmpty(surgDocId)) {
                CdcUserEntity cdcUserEntity = new CdcUserEntity();
                cdcUserEntity.setXcode(surgDocId);
                cdcUserEntity.setDataId(basedataId);
                cdcUserEntity.setIsActive(1);
                cdcUserEntity = cdcUserMapper.selectOne(cdcUserEntity);
                //判断人员存在不存在
                Long surgdoclongid = null;
                if (CommonUtil.isNotEmpty(cdcUserEntity)) {//不存在  保存到用户表里面
                    surgdoclongid = cdcUserEntity.getId();
                }
                sur.setSurgDocId(surgdoclongid);
            }
            //获取一助
            if (CommonUtil.isNotEmpty(assi1st)) {
                sur.setAssi1st(assi1st);
            }
            //获取术者
            if (CommonUtil.isNotEmpty(assi2nd)) {
                sur.setAssi2nd(assi2nd);
            }
            //获取SurgInciId
            if (CommonUtil.isNotEmpty(busSurgInciCode)) {
                CdcDictypeEntity cdcDictypeEntity = new CdcDictypeEntity();
                cdcDictypeEntity.setCode("bus_surg_inci");
                cdcDictypeEntity.setIsActive(1);
                cdcDictypeEntity = cdcDictypeMapper.selectOne(cdcDictypeEntity);
                Long dicsurginciId = cdcDictypeEntity.getId();
                //如果没有对应ID就要插入
                CdcDictionaryEntity cdcDictionaryEntity = new CdcDictionaryEntity();
                cdcDictionaryEntity.setDictypeId(dicsurginciId);
                cdcDictionaryEntity.setCode(busSurgInciCode);
                cdcDictionaryEntity.setIsActive(1);
                cdcDictionaryEntity = cdcDictionaryMapper.selectOne(cdcDictionaryEntity);
                //判断字典存在不存在
                Long bussurginciid = null;
                if (CommonUtil.isEmpty(cdcDictionaryEntity)) {//不存在  保存到字典表里面
                    cdcDictionaryEntity = new CdcDictionaryEntity();
                    cdcDictionaryEntity.setDictypeId(dicsurginciId);
                    cdcDictionaryEntity.setCode(busSurgInciCode);
                    cdcDictionaryEntity.setDescription(busSurgInciDesc);
                    cdcDictionaryEntity.setIsActive(1);
                    //保存字典
                    cdcDictionaryMapper.insert(cdcDictionaryEntity);
                    bussurginciid = cdcDictionaryEntity.getId();
                } else {
                    bussurginciid = cdcDictionaryEntity.getId();
                }
                sur.setBusSurgInciId(bussurginciid);
            }
            //获取surghealId
            if (CommonUtil.isNotEmpty(busSurgHealCode)) {
                CdcDictypeEntity cdcDictypeEntity = new CdcDictypeEntity();
                cdcDictypeEntity.setCode("bus_surg_heal");
                cdcDictypeEntity.setIsActive(1);
                cdcDictypeEntity = cdcDictypeMapper.selectOne(cdcDictypeEntity);
                Long dicsurghealId = cdcDictypeEntity.getId();
                //如果没有对应ID就要插入
                CdcDictionaryEntity cdcDictionaryEntity = new CdcDictionaryEntity();
                cdcDictionaryEntity.setDictypeId(dicsurghealId);
                cdcDictionaryEntity.setCode(busSurgHealCode);
                cdcDictionaryEntity.setIsActive(1);
                cdcDictionaryEntity = cdcDictionaryMapper.selectOne(cdcDictionaryEntity);
                //判断字典存在不存在
                Long bussurghealid = null;
                if (CommonUtil.isEmpty(cdcDictionaryEntity)) {//不存在  保存到字典表里面
                    cdcDictionaryEntity = new CdcDictionaryEntity();
                    cdcDictionaryEntity.setDictypeId(dicsurghealId);
                    cdcDictionaryEntity.setCode(busSurgHealCode);
                    cdcDictionaryEntity.setDescription(busSurgHealDesc);
                    cdcDictionaryEntity.setIsActive(1);
                    //保存字典
                    cdcDictionaryMapper.insert(cdcDictionaryEntity);
                    bussurghealid = cdcDictionaryEntity.getId();
                } else {
                    bussurghealid = cdcDictionaryEntity.getId();
                }
                sur.setBusSurgHealId(bussurghealid);
            }
            //获取surganesId
            if (CommonUtil.isNotEmpty(busSurgAnesCode)) {
                CdcDictypeEntity cdcDictypeEntity = new CdcDictypeEntity();
                cdcDictypeEntity.setCode("bus_surg_anes");
                cdcDictypeEntity.setIsActive(1);
                cdcDictypeEntity = cdcDictypeMapper.selectOne(cdcDictypeEntity);
                Long dicsurganesId = cdcDictypeEntity.getId();
                //如果没有对应ID就要插入
                CdcDictionaryEntity cdcDictionaryEntity = new CdcDictionaryEntity();
                cdcDictionaryEntity.setDictypeId(dicsurganesId);
                cdcDictionaryEntity.setCode(busSurgAnesCode);
                cdcDictionaryEntity.setIsActive(1);
                cdcDictionaryEntity = cdcDictionaryMapper.selectOne(cdcDictionaryEntity);
                //判断字典存在不存在
                Long bussurganesid = null;
                if (CommonUtil.isEmpty(cdcDictionaryEntity)) {//不存在  保存到字典表里面
                    cdcDictionaryEntity = new CdcDictionaryEntity();
                    cdcDictionaryEntity.setDictypeId(dicsurganesId);
                    cdcDictionaryEntity.setCode(busSurgAnesCode);
                    cdcDictionaryEntity.setDescription(busSurgAnesDesc);
                    cdcDictionaryEntity.setIsActive(1);
                    //保存字典
                    cdcDictionaryMapper.insert(cdcDictionaryEntity);
                    bussurganesid = cdcDictionaryEntity.getId();
                } else {
                    bussurganesid = cdcDictionaryEntity.getId();
                }
                sur.setBusSurgAnesId(bussurganesid);
            }
            //获取麻醉医师
            if (CommonUtil.isNotEmpty(anesDoc)) {
                sur.setAnesDoc(anesDoc);
            }
            if (CommonUtil.isNotEmpty(asaScore)) {
                sur.setAsaScore(asaScore);
            }
            if (CommonUtil.isNotEmpty(nnisGrade)) {
                sur.setNnisGrade(nnisGrade);
            }
            if (CommonUtil.isNotEmpty(wbcCount)) {
                Integer wbccountf = Integer.valueOf(wbcCount);
                sur.setWbcCount(wbccountf);
            }
            if (CommonUtil.isNotEmpty(inciCount)) {
                Integer incicountf = Integer.valueOf(inciCount);
                sur.setInciCount(incicountf);
            }
            if (CommonUtil.isNotEmpty(isEndoscope)) {
                Integer isendoscopef = Integer.valueOf(isEndoscope);
                sur.setIsEndoscope(isendoscopef);
            }
            if (CommonUtil.isNotEmpty(isImplants)) {
                Integer isimplantsf = Integer.valueOf(isImplants);
                sur.setIsImplants(isimplantsf);
            }
            if (CommonUtil.isNotEmpty(bloodLose)) {
                sur.setBloodLose(bloodLose);
            }
            if (CommonUtil.isNotEmpty(bloodGet)) {
                sur.setBloodGet(bloodGet);
            }
            if (CommonUtil.isNotEmpty(complication)) {
                sur.setComplication(complication);
            }
            //获取surgsourceId
            CdcDictypeEntity cdcDictypeEntity = new CdcDictypeEntity();
            cdcDictypeEntity.setCode("bus_surg_source");
            cdcDictypeEntity.setIsActive(1);
            cdcDictypeEntity = cdcDictypeMapper.selectOne(cdcDictypeEntity);
            Long dicsurgsourceId = cdcDictypeEntity.getId();
            //如果没有对应ID就要插入
            CdcDictionaryEntity cdcDictionaryEntity = new CdcDictionaryEntity();
            cdcDictionaryEntity.setDictypeId(dicsurgsourceId);
            cdcDictionaryEntity.setCode("C");
            cdcDictionaryEntity.setIsActive(1);
            cdcDictionaryEntity = cdcDictionaryMapper.selectOne(cdcDictionaryEntity);
            //判断字典存在不存在
            Long bussurgsourceid = null;
            if (CommonUtil.isEmpty(cdcDictionaryEntity)) {//不存在  保存到字典表里面
                cdcDictionaryEntity = new CdcDictionaryEntity();
                cdcDictionaryEntity.setDictypeId(dicsurgsourceId);
                cdcDictionaryEntity.setCode("C");
                cdcDictionaryEntity.setDescription("手术申请单");
                cdcDictionaryEntity.setIsActive(1);
                //保存字典
                cdcDictionaryMapper.insert(cdcDictionaryEntity);
                bussurgsourceid = cdcDictionaryEntity.getId();
            } else {
                bussurgsourceid = cdcDictionaryEntity.getId();
            }
            sur.setBusSurgSourceId(bussurgsourceid);
            sur.setDataId(dataId);
            sur.setIsActive(1);
            //先判断是不是有该对象
            Wrapper<CdcSurgeryEntity> surgWrapper = new EntityWrapper<CdcSurgeryEntity>();
            surgWrapper.eq("xcode", sur.getXcode());
            surgWrapper.eq("data_id", dataId);
            int count = cdcSurgeryService.selectCount(surgWrapper);
            if (count > 0) {
                cdcSurgeryService.update(sur, surgWrapper);
            } else {
                cdcSurgeryService.insert(sur);
            }
        //发送消息推送给前端刷新页面
        MyWebSocketHandler.fanoutMessage(episodeId);
    }

    @Override
    public void handleSignInfo(String dataStr) throws Exception{

            JSONObject dataJObj = JSONObject.parseObject(dataStr);
            String episodeId = dataJObj.getString("episode_id"); // 就诊记录
            //获取episodeid
            Wrapper<CdcEpisodeEntity> wrapper = new EntityWrapper<CdcEpisodeEntity>();
            wrapper.eq("xcode", episodeId);
            CdcEpisodeEntity cdcEpisodeEntity = cdcEpisodeService.selectOne(wrapper);//就诊信息没在库中？？？
            if (CommonUtil.isEmpty(cdcEpisodeEntity)) {
                return;
            }
            Long epsid = cdcEpisodeEntity.getId();
            //获取dataid
            CdcDataEntity cdcDataEntity = new CdcDataEntity();
            cdcDataEntity.setCode("SignData");
            cdcDataEntity.setIsActive(1);
            cdcDataEntity = cdcDataMapper.selectOne(cdcDataEntity);
            Long dataId = cdcDataEntity.getId();
            //获取basedataId
            CdcDataEntity cdcDataEntityBase = new CdcDataEntity();
            cdcDataEntityBase.setCode("HisBase");
            cdcDataEntityBase.setIsActive(1);
            cdcDataEntityBase = cdcDataMapper.selectOne(cdcDataEntityBase);
            Long basedataId = cdcDataEntityBase.getId();
            JSONArray labResultList = dataJObj.getJSONArray("SignList");
            for (int j = 0; j < labResultList.size(); j++) {
                JSONObject signResultObj = labResultList.getJSONObject(j);
                String code = signResultObj.getString("code"); // 代码
                String description = signResultObj.getString("description"); // 描述
                String value = signResultObj.getString("value"); // 值
                String checkDate = signResultObj.getString("check_date"); // 检测日期

                // 根据检测记录信息进行处理或存储
                CdcSignEntity sign = new CdcSignEntity();
                sign.setEpisodeId(epsid);
                sign.setCode(code);
                if (CommonUtil.isNotEmpty(checkDate)) {//不能为空
                    Date checkdatef = getTime(checkDate);
                    sign.setCheckDate(checkdatef);
                }

                if (CommonUtil.isNotEmpty(description)) {
                    sign.setDescription(description);
                }
                if (CommonUtil.isNotEmpty(value)) {
                    sign.setValue(value);
                }
                sign.setDataId(dataId);
                sign.setIsActive(1);

                // 先判断是不是有该对象
                Wrapper<CdcSignEntity> signWrapper = new EntityWrapper<CdcSignEntity>();
                signWrapper.eq("episode_id", sign.getEpisodeId());
                signWrapper.eq("code", sign.getCode());
                signWrapper.eq("data_id", dataId);
                signWrapper.eq("check_date", sign.getCheckDate());//同一代码  不同日期可重复
                int count = cdcSignService.selectCount(signWrapper);
                if (count > 0) {
                    cdcSignService.update(sign, signWrapper);
                } else {
                    cdcSignService.insert(sign);
                }
            }
        //发送消息推送给前端刷新页面
        MyWebSocketHandler.fanoutMessage(episodeId);
    }

    @Override
    public void handleNurEmrInfo(String dataStr) throws Exception{
            JSONObject dataJObj = JSONObject.parseObject(dataStr);
            String episodeId = dataJObj.getString("episode_id"); // 就诊记录
            String documentCode = dataJObj.getString("document_code"); // 病历模板代码
            String documentName = dataJObj.getString("document_name"); // 病历名称
            String nurDate = dataJObj.getString("nur_date"); // 填写日期

            //获取episodeid
            Wrapper<CdcEpisodeEntity> wrapper = new EntityWrapper<CdcEpisodeEntity>();
            wrapper.eq("xcode", episodeId);
            CdcEpisodeEntity cdcEpisodeEntity = cdcEpisodeService.selectOne(wrapper);//就诊信息没在库中？？？
            if (CommonUtil.isEmpty(cdcEpisodeEntity)) {
                return;
            }
            Long epsid = cdcEpisodeEntity.getId();
            //获取dataid
            CdcDataEntity cdcDataEntity = new CdcDataEntity();
            cdcDataEntity.setCode("NurData");
            cdcDataEntity.setIsActive(1);
            cdcDataEntity = cdcDataMapper.selectOne(cdcDataEntity);
            Long dataId = cdcDataEntity.getId();

            JSONArray itemList = dataJObj.getJSONArray("ItemList"); //项目列表
            for (int i = 0; i < itemList.size(); i++) {
                JSONObject itemObj = itemList.getJSONObject(i);
                String xcode = itemObj.getString("xcode"); // 病历小项代码
                String description = itemObj.getString("description"); // 病历小项描述
                String content = itemObj.getString("content"); // 病历小项内容

                // 根据病历记录信息进行处理或存储
                CdcNurEntity nur = new CdcNurEntity();
                nur.setEpisodeId(epsid);
                if (CommonUtil.isNotEmpty(description)) {
                    nur.setDescription(description);
                }
                if (CommonUtil.isNotEmpty(content)) {
                    nur.setContent(content);
                }
                if (CommonUtil.isNotEmpty(nurDate)) {
                    Date nurdatef = getTime(nurDate);
                    nur.setNurDate(nurdatef);
                }
                if (CommonUtil.isNotEmpty(documentCode)) {
                    nur.setDocCode(documentCode);
                }
                if (CommonUtil.isNotEmpty(documentName)) {
                    nur.setDocName(documentName);
                }
                if (CommonUtil.isNotEmpty(xcode)) {
                    nur.setXcode(xcode);
                }
                if (CommonUtil.isNotEmpty(description)) {
                    nur.setDescription(description);
                }
                if (CommonUtil.isNotEmpty(content)) {
                    nur.setContent(content);
                }
                nur.setDataId(dataId);
                nur.setIsActive(1);
                //先判断是不是有该对象
                Wrapper<CdcNurEntity> nurWrapper = new EntityWrapper<CdcNurEntity>();
                nurWrapper.eq("doc_code", nur.getDocCode());
                nurWrapper.eq("xcode", nur.getXcode());
                nurWrapper.eq("data_id", dataId);
                int count = cdcNurService.selectCount(nurWrapper);
                if (count > 0) {
                    cdcNurService.update(nur, nurWrapper);
                } else {
                    cdcNurService.insert(nur);
                }
            }
    }

    @Override
    public void handleFeeInfo(String dataStr) {
        JSONObject dataJObj = JSONObject.parseObject(dataStr);
    }

    /**
     * 日期字符串转时间
     *
     * @param datetime
     * @return
     */
    public static Date getTime(String datetime){
        if (CommonUtil.isEmpty(datetime)) {
            return null;
        }
        String pattern = "yyyy-MM-dd HH:mm:ss";
        if (com.dhcc.sds.util.DateUtil.isValidDate(datetime)) {
            pattern = "yyyy-MM-dd";
        } else if(com.dhcc.sds.util.DateUtil.isValidDateTime(datetime))  {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat format =  new SimpleDateFormat(pattern);
        Date date = null;
        try {
            date = format.parse(datetime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //日期
        return date;
    }
}
